Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Driver for Gateworks System Controller Hardware Monitor module
  4 *
  5 * Copyright (C) 2020 Gateworks Corporation
  6 */
  7#include <linux/hwmon.h>
  8#include <linux/hwmon-sysfs.h>
  9#include <linux/mfd/gsc.h>
 10#include <linux/module.h>
 11#include <linux/of.h>
 12#include <linux/platform_device.h>
 13#include <linux/regmap.h>
 14#include <linux/slab.h>
 15
 16#include <linux/platform_data/gsc_hwmon.h>
 17
 18#define GSC_HWMON_MAX_TEMP_CH	16
 19#define GSC_HWMON_MAX_IN_CH	16
 20#define GSC_HWMON_MAX_FAN_CH	16
 21
 22#define GSC_HWMON_RESOLUTION	12
 23#define GSC_HWMON_VREF		2500
 24
 25struct gsc_hwmon_data {
 26	struct gsc_dev *gsc;
 27	struct gsc_hwmon_platform_data *pdata;
 28	struct regmap *regmap;
 29	const struct gsc_hwmon_channel *temp_ch[GSC_HWMON_MAX_TEMP_CH];
 30	const struct gsc_hwmon_channel *in_ch[GSC_HWMON_MAX_IN_CH];
 31	const struct gsc_hwmon_channel *fan_ch[GSC_HWMON_MAX_FAN_CH];
 32	u32 temp_config[GSC_HWMON_MAX_TEMP_CH + 1];
 33	u32 in_config[GSC_HWMON_MAX_IN_CH + 1];
 34	u32 fan_config[GSC_HWMON_MAX_FAN_CH + 1];
 35	struct hwmon_channel_info temp_info;
 36	struct hwmon_channel_info in_info;
 37	struct hwmon_channel_info fan_info;
 38	const struct hwmon_channel_info *info[4];
 39	struct hwmon_chip_info chip;
 40};
 41
 42static const struct regmap_bus gsc_hwmon_regmap_bus = {
 43	.reg_read = gsc_read,
 44	.reg_write = gsc_write,
 45};
 46
 47static const struct regmap_config gsc_hwmon_regmap_config = {
 48	.reg_bits = 8,
 49	.val_bits = 8,
 50	.cache_type = REGCACHE_NONE,
 51};
 52
 53static ssize_t pwm_auto_point_temp_show(struct device *dev,
 54					struct device_attribute *devattr,
 55					char *buf)
 56{
 57	struct gsc_hwmon_data *hwmon = dev_get_drvdata(dev);
 58	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 59	u8 reg = hwmon->pdata->fan_base + (2 * attr->index);
 60	u8 regs[2];
 61	int ret;
 62
 63	ret = regmap_bulk_read(hwmon->regmap, reg, regs, 2);
 64	if (ret)
 65		return ret;
 66
 67	ret = regs[0] | regs[1] << 8;
 68	return sprintf(buf, "%d\n", ret * 10);
 69}
 70
 71static ssize_t pwm_auto_point_temp_store(struct device *dev,
 72					 struct device_attribute *devattr,
 73					 const char *buf, size_t count)
 74{
 75	struct gsc_hwmon_data *hwmon = dev_get_drvdata(dev);
 76	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 77	u8 reg = hwmon->pdata->fan_base + (2 * attr->index);
 78	u8 regs[2];
 79	long temp;
 80	int err;
 81
 82	if (kstrtol(buf, 10, &temp))
 83		return -EINVAL;
 84
 85	temp = clamp_val(temp, 0, 100000);
 86	temp = DIV_ROUND_CLOSEST(temp, 100);
 87
 88	regs[0] = temp & 0xff;
 89	regs[1] = (temp >> 8) & 0xff;
 90	err = regmap_bulk_write(hwmon->regmap, reg, regs, 2);
 91	if (err)
 92		return err;
 93
 94	return count;
 95}
 96
 97static ssize_t pwm_auto_point_pwm_show(struct device *dev,
 98				       struct device_attribute *devattr,
 99				       char *buf)
100{
101	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
102
103	return sprintf(buf, "%d\n", 255 * (50 + (attr->index * 10)));
104}
105
106static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point1_pwm, pwm_auto_point_pwm, 0);
107static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point1_temp, pwm_auto_point_temp, 0);
108
109static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point2_pwm, pwm_auto_point_pwm, 1);
110static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point2_temp, pwm_auto_point_temp, 1);
111
112static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point3_pwm, pwm_auto_point_pwm, 2);
113static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point3_temp, pwm_auto_point_temp, 2);
114
115static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point4_pwm, pwm_auto_point_pwm, 3);
116static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point4_temp, pwm_auto_point_temp, 3);
117
118static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point5_pwm, pwm_auto_point_pwm, 4);
119static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point5_temp, pwm_auto_point_temp, 4);
120
121static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point6_pwm, pwm_auto_point_pwm, 5);
122static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point6_temp, pwm_auto_point_temp, 5);
123
124static struct attribute *gsc_hwmon_attributes[] = {
125	&sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
126	&sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
127	&sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
128	&sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
129	&sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
130	&sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
131	&sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
132	&sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
133	&sensor_dev_attr_pwm1_auto_point5_pwm.dev_attr.attr,
134	&sensor_dev_attr_pwm1_auto_point5_temp.dev_attr.attr,
135	&sensor_dev_attr_pwm1_auto_point6_pwm.dev_attr.attr,
136	&sensor_dev_attr_pwm1_auto_point6_temp.dev_attr.attr,
137	NULL
138};
139
140static const struct attribute_group gsc_hwmon_group = {
141	.attrs = gsc_hwmon_attributes,
142};
143__ATTRIBUTE_GROUPS(gsc_hwmon);
144
145static int
146gsc_hwmon_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
147	       int channel, long *val)
148{
149	struct gsc_hwmon_data *hwmon = dev_get_drvdata(dev);
150	const struct gsc_hwmon_channel *ch;
151	int sz, ret;
152	long tmp;
153	u8 buf[3];
154
155	switch (type) {
156	case hwmon_in:
157		ch = hwmon->in_ch[channel];
158		break;
159	case hwmon_temp:
160		ch = hwmon->temp_ch[channel];
161		break;
162	case hwmon_fan:
163		ch = hwmon->fan_ch[channel];
164		break;
165	default:
166		return -EOPNOTSUPP;
167	}
168
169	sz = (ch->mode == mode_voltage_24bit) ? 3 : 2;
170	ret = regmap_bulk_read(hwmon->regmap, ch->reg, buf, sz);
171	if (ret)
172		return ret;
173
174	tmp = 0;
175	while (sz-- > 0)
176		tmp |= (buf[sz] << (8 * sz));
177
178	switch (ch->mode) {
179	case mode_temperature:
180		if (tmp > 0x8000)
181			tmp -= 0xffff;
182		tmp *= 100; /* convert to millidegrees celsius */
183		break;
184	case mode_voltage_raw:
185		tmp = clamp_val(tmp, 0, BIT(GSC_HWMON_RESOLUTION));
186		/* scale based on ref voltage and ADC resolution */
187		tmp *= GSC_HWMON_VREF;
188		tmp >>= GSC_HWMON_RESOLUTION;
189		/* scale based on optional voltage divider */
190		if (ch->vdiv[0] && ch->vdiv[1]) {
191			tmp *= (ch->vdiv[0] + ch->vdiv[1]);
192			tmp /= ch->vdiv[1];
193		}
194		/* adjust by uV offset */
195		tmp += ch->mvoffset;
196		break;
197	case mode_fan:
198		tmp *= 30; /* convert to revolutions per minute */
199		break;
200	case mode_voltage_24bit:
201	case mode_voltage_16bit:
202		/* no adjustment needed */
203		break;
204	}
205
206	*val = tmp;
207
208	return 0;
209}
210
211static int
212gsc_hwmon_read_string(struct device *dev, enum hwmon_sensor_types type,
213		      u32 attr, int channel, const char **buf)
214{
215	struct gsc_hwmon_data *hwmon = dev_get_drvdata(dev);
216
217	switch (type) {
218	case hwmon_in:
219		*buf = hwmon->in_ch[channel]->name;
220		break;
221	case hwmon_temp:
222		*buf = hwmon->temp_ch[channel]->name;
223		break;
224	case hwmon_fan:
225		*buf = hwmon->fan_ch[channel]->name;
226		break;
227	default:
228		return -ENOTSUPP;
229	}
230
231	return 0;
232}
233
 
 
 
 
 
 
 
234static const struct hwmon_ops gsc_hwmon_ops = {
235	.visible = 0444,
236	.read = gsc_hwmon_read,
237	.read_string = gsc_hwmon_read_string,
238};
239
240static struct gsc_hwmon_platform_data *
241gsc_hwmon_get_devtree_pdata(struct device *dev)
242{
243	struct gsc_hwmon_platform_data *pdata;
244	struct gsc_hwmon_channel *ch;
 
245	struct device_node *fan;
246	int nchannels;
247
248	nchannels = device_get_child_node_count(dev);
249	if (nchannels == 0)
250		return ERR_PTR(-ENODEV);
251
252	pdata = devm_kzalloc(dev, struct_size(pdata, channels, nchannels),
 
253			     GFP_KERNEL);
254	if (!pdata)
255		return ERR_PTR(-ENOMEM);
 
 
256	pdata->nchannels = nchannels;
257
258	/* fan controller base address */
259	of_node_get(dev->parent->of_node);
260	fan = of_find_compatible_node(dev->parent->of_node, NULL, "gw,gsc-fan");
261	if (fan && of_property_read_u32(fan, "reg", &pdata->fan_base)) {
262		of_node_put(fan);
263		dev_err(dev, "fan node without base\n");
264		return ERR_PTR(-EINVAL);
265	}
266
267	of_node_put(fan);
268
269	ch = pdata->channels;
270	/* allocate structures for channels and count instances of each type */
271	device_for_each_child_node_scoped(dev, child) {
272		if (fwnode_property_read_string(child, "label", &ch->name)) {
273			dev_err(dev, "channel without label\n");
 
274			return ERR_PTR(-EINVAL);
275		}
276		if (fwnode_property_read_u32(child, "reg", &ch->reg)) {
277			dev_err(dev, "channel without reg\n");
 
278			return ERR_PTR(-EINVAL);
279		}
280		if (fwnode_property_read_u32(child, "gw,mode", &ch->mode)) {
281			dev_err(dev, "channel without mode\n");
 
282			return ERR_PTR(-EINVAL);
283		}
284		if (ch->mode > mode_max) {
285			dev_err(dev, "invalid channel mode\n");
 
286			return ERR_PTR(-EINVAL);
287		}
288
289		if (!fwnode_property_read_u32(child,
290					      "gw,voltage-offset-microvolt",
291					      &ch->mvoffset))
292			ch->mvoffset /= 1000;
293		fwnode_property_read_u32_array(child,
294					       "gw,voltage-divider-ohms",
295					       ch->vdiv, ARRAY_SIZE(ch->vdiv));
296		ch++;
297	}
298
299	return pdata;
300}
301
302static int gsc_hwmon_probe(struct platform_device *pdev)
303{
304	struct gsc_dev *gsc = dev_get_drvdata(pdev->dev.parent);
305	struct device *dev = &pdev->dev;
306	struct device *hwmon_dev;
307	struct gsc_hwmon_platform_data *pdata = dev_get_platdata(dev);
308	struct gsc_hwmon_data *hwmon;
309	const struct attribute_group **groups;
310	int i, i_in, i_temp, i_fan;
311
312	if (!pdata) {
313		pdata = gsc_hwmon_get_devtree_pdata(dev);
314		if (IS_ERR(pdata))
315			return PTR_ERR(pdata);
316	}
317
318	hwmon = devm_kzalloc(dev, sizeof(*hwmon), GFP_KERNEL);
319	if (!hwmon)
320		return -ENOMEM;
321	hwmon->gsc = gsc;
322	hwmon->pdata = pdata;
323
324	hwmon->regmap = devm_regmap_init(dev, &gsc_hwmon_regmap_bus,
325					 gsc->i2c_hwmon,
326					 &gsc_hwmon_regmap_config);
327	if (IS_ERR(hwmon->regmap))
328		return PTR_ERR(hwmon->regmap);
329
330	for (i = 0, i_in = 0, i_temp = 0, i_fan = 0; i < hwmon->pdata->nchannels; i++) {
331		const struct gsc_hwmon_channel *ch = &pdata->channels[i];
332
333		switch (ch->mode) {
334		case mode_temperature:
335			if (i_temp == GSC_HWMON_MAX_TEMP_CH) {
336				dev_err(gsc->dev, "too many temp channels\n");
337				return -EINVAL;
338			}
339			hwmon->temp_ch[i_temp] = ch;
340			hwmon->temp_config[i_temp] = HWMON_T_INPUT |
341						     HWMON_T_LABEL;
342			i_temp++;
343			break;
344		case mode_fan:
345			if (i_fan == GSC_HWMON_MAX_FAN_CH) {
346				dev_err(gsc->dev, "too many fan channels\n");
347				return -EINVAL;
348			}
349			hwmon->fan_ch[i_fan] = ch;
350			hwmon->fan_config[i_fan] = HWMON_F_INPUT |
351						   HWMON_F_LABEL;
352			i_fan++;
353			break;
354		case mode_voltage_24bit:
355		case mode_voltage_16bit:
356		case mode_voltage_raw:
357			if (i_in == GSC_HWMON_MAX_IN_CH) {
358				dev_err(gsc->dev, "too many input channels\n");
359				return -EINVAL;
360			}
361			hwmon->in_ch[i_in] = ch;
362			hwmon->in_config[i_in] =
363				HWMON_I_INPUT | HWMON_I_LABEL;
364			i_in++;
365			break;
366		default:
367			dev_err(gsc->dev, "invalid mode: %d\n", ch->mode);
368			return -EINVAL;
369		}
370	}
371
372	/* setup config structures */
373	hwmon->chip.ops = &gsc_hwmon_ops;
374	hwmon->chip.info = hwmon->info;
375	hwmon->info[0] = &hwmon->temp_info;
376	hwmon->info[1] = &hwmon->in_info;
377	hwmon->info[2] = &hwmon->fan_info;
378	hwmon->temp_info.type = hwmon_temp;
379	hwmon->temp_info.config = hwmon->temp_config;
380	hwmon->in_info.type = hwmon_in;
381	hwmon->in_info.config = hwmon->in_config;
382	hwmon->fan_info.type = hwmon_fan;
383	hwmon->fan_info.config = hwmon->fan_config;
384
385	groups = pdata->fan_base ? gsc_hwmon_groups : NULL;
386	hwmon_dev = devm_hwmon_device_register_with_info(dev,
387							 KBUILD_MODNAME, hwmon,
388							 &hwmon->chip, groups);
389	return PTR_ERR_OR_ZERO(hwmon_dev);
390}
391
392static const struct of_device_id gsc_hwmon_of_match[] = {
393	{ .compatible = "gw,gsc-adc", },
394	{}
395};
396MODULE_DEVICE_TABLE(of, gsc_hwmon_of_match);
397
398static struct platform_driver gsc_hwmon_driver = {
399	.driver = {
400		.name = "gsc-hwmon",
401		.of_match_table = gsc_hwmon_of_match,
402	},
403	.probe = gsc_hwmon_probe,
404};
405
406module_platform_driver(gsc_hwmon_driver);
407
408MODULE_AUTHOR("Tim Harvey <tharvey@gateworks.com>");
409MODULE_DESCRIPTION("GSC hardware monitor driver");
410MODULE_LICENSE("GPL v2");
v5.9
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Driver for Gateworks System Controller Hardware Monitor module
  4 *
  5 * Copyright (C) 2020 Gateworks Corporation
  6 */
  7#include <linux/hwmon.h>
  8#include <linux/hwmon-sysfs.h>
  9#include <linux/mfd/gsc.h>
 10#include <linux/module.h>
 11#include <linux/of.h>
 12#include <linux/platform_device.h>
 13#include <linux/regmap.h>
 14#include <linux/slab.h>
 15
 16#include <linux/platform_data/gsc_hwmon.h>
 17
 18#define GSC_HWMON_MAX_TEMP_CH	16
 19#define GSC_HWMON_MAX_IN_CH	16
 
 20
 21#define GSC_HWMON_RESOLUTION	12
 22#define GSC_HWMON_VREF		2500
 23
 24struct gsc_hwmon_data {
 25	struct gsc_dev *gsc;
 26	struct gsc_hwmon_platform_data *pdata;
 27	struct regmap *regmap;
 28	const struct gsc_hwmon_channel *temp_ch[GSC_HWMON_MAX_TEMP_CH];
 29	const struct gsc_hwmon_channel *in_ch[GSC_HWMON_MAX_IN_CH];
 
 30	u32 temp_config[GSC_HWMON_MAX_TEMP_CH + 1];
 31	u32 in_config[GSC_HWMON_MAX_IN_CH + 1];
 
 32	struct hwmon_channel_info temp_info;
 33	struct hwmon_channel_info in_info;
 34	const struct hwmon_channel_info *info[3];
 
 35	struct hwmon_chip_info chip;
 36};
 37
 38static struct regmap_bus gsc_hwmon_regmap_bus = {
 39	.reg_read = gsc_read,
 40	.reg_write = gsc_write,
 41};
 42
 43static const struct regmap_config gsc_hwmon_regmap_config = {
 44	.reg_bits = 8,
 45	.val_bits = 8,
 46	.cache_type = REGCACHE_NONE,
 47};
 48
 49static ssize_t pwm_auto_point_temp_show(struct device *dev,
 50					struct device_attribute *devattr,
 51					char *buf)
 52{
 53	struct gsc_hwmon_data *hwmon = dev_get_drvdata(dev);
 54	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 55	u8 reg = hwmon->pdata->fan_base + (2 * attr->index);
 56	u8 regs[2];
 57	int ret;
 58
 59	ret = regmap_bulk_read(hwmon->regmap, reg, regs, 2);
 60	if (ret)
 61		return ret;
 62
 63	ret = regs[0] | regs[1] << 8;
 64	return sprintf(buf, "%d\n", ret * 10);
 65}
 66
 67static ssize_t pwm_auto_point_temp_store(struct device *dev,
 68					 struct device_attribute *devattr,
 69					 const char *buf, size_t count)
 70{
 71	struct gsc_hwmon_data *hwmon = dev_get_drvdata(dev);
 72	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 73	u8 reg = hwmon->pdata->fan_base + (2 * attr->index);
 74	u8 regs[2];
 75	long temp;
 76	int err;
 77
 78	if (kstrtol(buf, 10, &temp))
 79		return -EINVAL;
 80
 81	temp = clamp_val(temp, 0, 10000);
 82	temp = DIV_ROUND_CLOSEST(temp, 10);
 83
 84	regs[0] = temp & 0xff;
 85	regs[1] = (temp >> 8) & 0xff;
 86	err = regmap_bulk_write(hwmon->regmap, reg, regs, 2);
 87	if (err)
 88		return err;
 89
 90	return count;
 91}
 92
 93static ssize_t pwm_auto_point_pwm_show(struct device *dev,
 94				       struct device_attribute *devattr,
 95				       char *buf)
 96{
 97	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 98
 99	return sprintf(buf, "%d\n", 255 * (50 + (attr->index * 10)) / 100);
100}
101
102static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point1_pwm, pwm_auto_point_pwm, 0);
103static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point1_temp, pwm_auto_point_temp, 0);
104
105static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point2_pwm, pwm_auto_point_pwm, 1);
106static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point2_temp, pwm_auto_point_temp, 1);
107
108static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point3_pwm, pwm_auto_point_pwm, 2);
109static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point3_temp, pwm_auto_point_temp, 2);
110
111static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point4_pwm, pwm_auto_point_pwm, 3);
112static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point4_temp, pwm_auto_point_temp, 3);
113
114static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point5_pwm, pwm_auto_point_pwm, 4);
115static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point5_temp, pwm_auto_point_temp, 4);
116
117static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point6_pwm, pwm_auto_point_pwm, 5);
118static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point6_temp, pwm_auto_point_temp, 5);
119
120static struct attribute *gsc_hwmon_attributes[] = {
121	&sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
122	&sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
123	&sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
124	&sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
125	&sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
126	&sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
127	&sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
128	&sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
129	&sensor_dev_attr_pwm1_auto_point5_pwm.dev_attr.attr,
130	&sensor_dev_attr_pwm1_auto_point5_temp.dev_attr.attr,
131	&sensor_dev_attr_pwm1_auto_point6_pwm.dev_attr.attr,
132	&sensor_dev_attr_pwm1_auto_point6_temp.dev_attr.attr,
133	NULL
134};
135
136static const struct attribute_group gsc_hwmon_group = {
137	.attrs = gsc_hwmon_attributes,
138};
139__ATTRIBUTE_GROUPS(gsc_hwmon);
140
141static int
142gsc_hwmon_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
143	       int channel, long *val)
144{
145	struct gsc_hwmon_data *hwmon = dev_get_drvdata(dev);
146	const struct gsc_hwmon_channel *ch;
147	int sz, ret;
148	long tmp;
149	u8 buf[3];
150
151	switch (type) {
152	case hwmon_in:
153		ch = hwmon->in_ch[channel];
154		break;
155	case hwmon_temp:
156		ch = hwmon->temp_ch[channel];
157		break;
 
 
 
158	default:
159		return -EOPNOTSUPP;
160	}
161
162	sz = (ch->mode == mode_voltage_24bit) ? 3 : 2;
163	ret = regmap_bulk_read(hwmon->regmap, ch->reg, buf, sz);
164	if (ret)
165		return ret;
166
167	tmp = 0;
168	while (sz-- > 0)
169		tmp |= (buf[sz] << (8 * sz));
170
171	switch (ch->mode) {
172	case mode_temperature:
173		if (tmp > 0x8000)
174			tmp -= 0xffff;
175		tmp *= 100; /* convert to millidegrees celsius */
176		break;
177	case mode_voltage_raw:
178		tmp = clamp_val(tmp, 0, BIT(GSC_HWMON_RESOLUTION));
179		/* scale based on ref voltage and ADC resolution */
180		tmp *= GSC_HWMON_VREF;
181		tmp >>= GSC_HWMON_RESOLUTION;
182		/* scale based on optional voltage divider */
183		if (ch->vdiv[0] && ch->vdiv[1]) {
184			tmp *= (ch->vdiv[0] + ch->vdiv[1]);
185			tmp /= ch->vdiv[1];
186		}
187		/* adjust by uV offset */
188		tmp += ch->mvoffset;
189		break;
 
 
 
190	case mode_voltage_24bit:
191	case mode_voltage_16bit:
192		/* no adjustment needed */
193		break;
194	}
195
196	*val = tmp;
197
198	return 0;
199}
200
201static int
202gsc_hwmon_read_string(struct device *dev, enum hwmon_sensor_types type,
203		      u32 attr, int channel, const char **buf)
204{
205	struct gsc_hwmon_data *hwmon = dev_get_drvdata(dev);
206
207	switch (type) {
208	case hwmon_in:
209		*buf = hwmon->in_ch[channel]->name;
210		break;
211	case hwmon_temp:
212		*buf = hwmon->temp_ch[channel]->name;
213		break;
 
 
 
214	default:
215		return -ENOTSUPP;
216	}
217
218	return 0;
219}
220
221static umode_t
222gsc_hwmon_is_visible(const void *_data, enum hwmon_sensor_types type, u32 attr,
223		     int ch)
224{
225	return 0444;
226}
227
228static const struct hwmon_ops gsc_hwmon_ops = {
229	.is_visible = gsc_hwmon_is_visible,
230	.read = gsc_hwmon_read,
231	.read_string = gsc_hwmon_read_string,
232};
233
234static struct gsc_hwmon_platform_data *
235gsc_hwmon_get_devtree_pdata(struct device *dev)
236{
237	struct gsc_hwmon_platform_data *pdata;
238	struct gsc_hwmon_channel *ch;
239	struct fwnode_handle *child;
240	struct device_node *fan;
241	int nchannels;
242
243	nchannels = device_get_child_node_count(dev);
244	if (nchannels == 0)
245		return ERR_PTR(-ENODEV);
246
247	pdata = devm_kzalloc(dev,
248			     sizeof(*pdata) + nchannels * sizeof(*ch),
249			     GFP_KERNEL);
250	if (!pdata)
251		return ERR_PTR(-ENOMEM);
252	ch = (struct gsc_hwmon_channel *)(pdata + 1);
253	pdata->channels = ch;
254	pdata->nchannels = nchannels;
255
256	/* fan controller base address */
 
257	fan = of_find_compatible_node(dev->parent->of_node, NULL, "gw,gsc-fan");
258	if (fan && of_property_read_u32(fan, "reg", &pdata->fan_base)) {
 
259		dev_err(dev, "fan node without base\n");
260		return ERR_PTR(-EINVAL);
261	}
262
 
 
 
263	/* allocate structures for channels and count instances of each type */
264	device_for_each_child_node(dev, child) {
265		if (fwnode_property_read_string(child, "label", &ch->name)) {
266			dev_err(dev, "channel without label\n");
267			fwnode_handle_put(child);
268			return ERR_PTR(-EINVAL);
269		}
270		if (fwnode_property_read_u32(child, "reg", &ch->reg)) {
271			dev_err(dev, "channel without reg\n");
272			fwnode_handle_put(child);
273			return ERR_PTR(-EINVAL);
274		}
275		if (fwnode_property_read_u32(child, "gw,mode", &ch->mode)) {
276			dev_err(dev, "channel without mode\n");
277			fwnode_handle_put(child);
278			return ERR_PTR(-EINVAL);
279		}
280		if (ch->mode > mode_max) {
281			dev_err(dev, "invalid channel mode\n");
282			fwnode_handle_put(child);
283			return ERR_PTR(-EINVAL);
284		}
285
286		if (!fwnode_property_read_u32(child,
287					      "gw,voltage-offset-microvolt",
288					      &ch->mvoffset))
289			ch->mvoffset /= 1000;
290		fwnode_property_read_u32_array(child,
291					       "gw,voltage-divider-ohms",
292					       ch->vdiv, ARRAY_SIZE(ch->vdiv));
293		ch++;
294	}
295
296	return pdata;
297}
298
299static int gsc_hwmon_probe(struct platform_device *pdev)
300{
301	struct gsc_dev *gsc = dev_get_drvdata(pdev->dev.parent);
302	struct device *dev = &pdev->dev;
303	struct device *hwmon_dev;
304	struct gsc_hwmon_platform_data *pdata = dev_get_platdata(dev);
305	struct gsc_hwmon_data *hwmon;
306	const struct attribute_group **groups;
307	int i, i_in, i_temp;
308
309	if (!pdata) {
310		pdata = gsc_hwmon_get_devtree_pdata(dev);
311		if (IS_ERR(pdata))
312			return PTR_ERR(pdata);
313	}
314
315	hwmon = devm_kzalloc(dev, sizeof(*hwmon), GFP_KERNEL);
316	if (!hwmon)
317		return -ENOMEM;
318	hwmon->gsc = gsc;
319	hwmon->pdata = pdata;
320
321	hwmon->regmap = devm_regmap_init(dev, &gsc_hwmon_regmap_bus,
322					 gsc->i2c_hwmon,
323					 &gsc_hwmon_regmap_config);
324	if (IS_ERR(hwmon->regmap))
325		return PTR_ERR(hwmon->regmap);
326
327	for (i = 0, i_in = 0, i_temp = 0; i < hwmon->pdata->nchannels; i++) {
328		const struct gsc_hwmon_channel *ch = &pdata->channels[i];
329
330		switch (ch->mode) {
331		case mode_temperature:
332			if (i_temp == GSC_HWMON_MAX_TEMP_CH) {
333				dev_err(gsc->dev, "too many temp channels\n");
334				return -EINVAL;
335			}
336			hwmon->temp_ch[i_temp] = ch;
337			hwmon->temp_config[i_temp] = HWMON_T_INPUT |
338						     HWMON_T_LABEL;
339			i_temp++;
340			break;
 
 
 
 
 
 
 
 
 
 
341		case mode_voltage_24bit:
342		case mode_voltage_16bit:
343		case mode_voltage_raw:
344			if (i_in == GSC_HWMON_MAX_IN_CH) {
345				dev_err(gsc->dev, "too many input channels\n");
346				return -EINVAL;
347			}
348			hwmon->in_ch[i_in] = ch;
349			hwmon->in_config[i_in] =
350				HWMON_I_INPUT | HWMON_I_LABEL;
351			i_in++;
352			break;
353		default:
354			dev_err(gsc->dev, "invalid mode: %d\n", ch->mode);
355			return -EINVAL;
356		}
357	}
358
359	/* setup config structures */
360	hwmon->chip.ops = &gsc_hwmon_ops;
361	hwmon->chip.info = hwmon->info;
362	hwmon->info[0] = &hwmon->temp_info;
363	hwmon->info[1] = &hwmon->in_info;
 
364	hwmon->temp_info.type = hwmon_temp;
365	hwmon->temp_info.config = hwmon->temp_config;
366	hwmon->in_info.type = hwmon_in;
367	hwmon->in_info.config = hwmon->in_config;
 
 
368
369	groups = pdata->fan_base ? gsc_hwmon_groups : NULL;
370	hwmon_dev = devm_hwmon_device_register_with_info(dev,
371							 KBUILD_MODNAME, hwmon,
372							 &hwmon->chip, groups);
373	return PTR_ERR_OR_ZERO(hwmon_dev);
374}
375
376static const struct of_device_id gsc_hwmon_of_match[] = {
377	{ .compatible = "gw,gsc-adc", },
378	{}
379};
 
380
381static struct platform_driver gsc_hwmon_driver = {
382	.driver = {
383		.name = "gsc-hwmon",
384		.of_match_table = gsc_hwmon_of_match,
385	},
386	.probe = gsc_hwmon_probe,
387};
388
389module_platform_driver(gsc_hwmon_driver);
390
391MODULE_AUTHOR("Tim Harvey <tharvey@gateworks.com>");
392MODULE_DESCRIPTION("GSC hardware monitor driver");
393MODULE_LICENSE("GPL v2");