xref: /linux/drivers/hwmon/gsc-hwmon.c (revision 95298d63c67673c654c08952672d016212b26054)
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 
24 struct 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 
38 static struct regmap_bus gsc_hwmon_regmap_bus = {
39 	.reg_read = gsc_read,
40 	.reg_write = gsc_write,
41 };
42 
43 static const struct regmap_config gsc_hwmon_regmap_config = {
44 	.reg_bits = 8,
45 	.val_bits = 8,
46 	.cache_type = REGCACHE_NONE,
47 };
48 
49 static 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 
67 static 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 
93 static 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 
102 static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point1_pwm, pwm_auto_point_pwm, 0);
103 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point1_temp, pwm_auto_point_temp, 0);
104 
105 static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point2_pwm, pwm_auto_point_pwm, 1);
106 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point2_temp, pwm_auto_point_temp, 1);
107 
108 static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point3_pwm, pwm_auto_point_pwm, 2);
109 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point3_temp, pwm_auto_point_temp, 2);
110 
111 static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point4_pwm, pwm_auto_point_pwm, 3);
112 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point4_temp, pwm_auto_point_temp, 3);
113 
114 static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point5_pwm, pwm_auto_point_pwm, 4);
115 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point5_temp, pwm_auto_point_temp, 4);
116 
117 static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point6_pwm, pwm_auto_point_pwm, 5);
118 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point6_temp, pwm_auto_point_temp, 5);
119 
120 static 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 
136 static const struct attribute_group gsc_hwmon_group = {
137 	.attrs = gsc_hwmon_attributes,
138 };
139 __ATTRIBUTE_GROUPS(gsc_hwmon);
140 
141 static int
142 gsc_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) ? 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 		break;
176 	case mode_voltage_raw:
177 		tmp = clamp_val(tmp, 0, BIT(GSC_HWMON_RESOLUTION));
178 		/* scale based on ref voltage and ADC resolution */
179 		tmp *= GSC_HWMON_VREF;
180 		tmp >>= GSC_HWMON_RESOLUTION;
181 		/* scale based on optional voltage divider */
182 		if (ch->vdiv[0] && ch->vdiv[1]) {
183 			tmp *= (ch->vdiv[0] + ch->vdiv[1]);
184 			tmp /= ch->vdiv[1];
185 		}
186 		/* adjust by uV offset */
187 		tmp += ch->mvoffset;
188 		break;
189 	case mode_voltage:
190 		/* no adjustment needed */
191 		break;
192 	}
193 
194 	*val = tmp;
195 
196 	return 0;
197 }
198 
199 static int
200 gsc_hwmon_read_string(struct device *dev, enum hwmon_sensor_types type,
201 		      u32 attr, int channel, const char **buf)
202 {
203 	struct gsc_hwmon_data *hwmon = dev_get_drvdata(dev);
204 
205 	switch (type) {
206 	case hwmon_in:
207 		*buf = hwmon->in_ch[channel]->name;
208 		break;
209 	case hwmon_temp:
210 		*buf = hwmon->temp_ch[channel]->name;
211 		break;
212 	default:
213 		return -ENOTSUPP;
214 	}
215 
216 	return 0;
217 }
218 
219 static umode_t
220 gsc_hwmon_is_visible(const void *_data, enum hwmon_sensor_types type, u32 attr,
221 		     int ch)
222 {
223 	return 0444;
224 }
225 
226 static const struct hwmon_ops gsc_hwmon_ops = {
227 	.is_visible = gsc_hwmon_is_visible,
228 	.read = gsc_hwmon_read,
229 	.read_string = gsc_hwmon_read_string,
230 };
231 
232 static struct gsc_hwmon_platform_data *
233 gsc_hwmon_get_devtree_pdata(struct device *dev)
234 {
235 	struct gsc_hwmon_platform_data *pdata;
236 	struct gsc_hwmon_channel *ch;
237 	struct fwnode_handle *child;
238 	struct device_node *fan;
239 	int nchannels;
240 
241 	nchannels = device_get_child_node_count(dev);
242 	if (nchannels == 0)
243 		return ERR_PTR(-ENODEV);
244 
245 	pdata = devm_kzalloc(dev,
246 			     sizeof(*pdata) + nchannels * sizeof(*ch),
247 			     GFP_KERNEL);
248 	if (!pdata)
249 		return ERR_PTR(-ENOMEM);
250 	ch = (struct gsc_hwmon_channel *)(pdata + 1);
251 	pdata->channels = ch;
252 	pdata->nchannels = nchannels;
253 
254 	/* fan controller base address */
255 	fan = of_find_compatible_node(dev->parent->of_node, NULL, "gw,gsc-fan");
256 	if (fan && of_property_read_u32(fan, "reg", &pdata->fan_base)) {
257 		dev_err(dev, "fan node without base\n");
258 		return ERR_PTR(-EINVAL);
259 	}
260 
261 	/* allocate structures for channels and count instances of each type */
262 	device_for_each_child_node(dev, child) {
263 		if (fwnode_property_read_string(child, "label", &ch->name)) {
264 			dev_err(dev, "channel without label\n");
265 			fwnode_handle_put(child);
266 			return ERR_PTR(-EINVAL);
267 		}
268 		if (fwnode_property_read_u32(child, "reg", &ch->reg)) {
269 			dev_err(dev, "channel without reg\n");
270 			fwnode_handle_put(child);
271 			return ERR_PTR(-EINVAL);
272 		}
273 		if (fwnode_property_read_u32(child, "gw,mode", &ch->mode)) {
274 			dev_err(dev, "channel without mode\n");
275 			fwnode_handle_put(child);
276 			return ERR_PTR(-EINVAL);
277 		}
278 		if (ch->mode > mode_max) {
279 			dev_err(dev, "invalid channel mode\n");
280 			fwnode_handle_put(child);
281 			return ERR_PTR(-EINVAL);
282 		}
283 
284 		if (!fwnode_property_read_u32(child,
285 					      "gw,voltage-offset-microvolt",
286 					      &ch->mvoffset))
287 			ch->mvoffset /= 1000;
288 		fwnode_property_read_u32_array(child,
289 					       "gw,voltage-divider-ohms",
290 					       ch->vdiv, ARRAY_SIZE(ch->vdiv));
291 		ch++;
292 	}
293 
294 	return pdata;
295 }
296 
297 static int gsc_hwmon_probe(struct platform_device *pdev)
298 {
299 	struct gsc_dev *gsc = dev_get_drvdata(pdev->dev.parent);
300 	struct device *dev = &pdev->dev;
301 	struct device *hwmon_dev;
302 	struct gsc_hwmon_platform_data *pdata = dev_get_platdata(dev);
303 	struct gsc_hwmon_data *hwmon;
304 	const struct attribute_group **groups;
305 	int i, i_in, i_temp;
306 
307 	if (!pdata) {
308 		pdata = gsc_hwmon_get_devtree_pdata(dev);
309 		if (IS_ERR(pdata))
310 			return PTR_ERR(pdata);
311 	}
312 
313 	hwmon = devm_kzalloc(dev, sizeof(*hwmon), GFP_KERNEL);
314 	if (!hwmon)
315 		return -ENOMEM;
316 	hwmon->gsc = gsc;
317 	hwmon->pdata = pdata;
318 
319 	hwmon->regmap = devm_regmap_init(dev, &gsc_hwmon_regmap_bus,
320 					 gsc->i2c_hwmon,
321 					 &gsc_hwmon_regmap_config);
322 	if (IS_ERR(hwmon->regmap))
323 		return PTR_ERR(hwmon->regmap);
324 
325 	for (i = 0, i_in = 0, i_temp = 0; i < hwmon->pdata->nchannels; i++) {
326 		const struct gsc_hwmon_channel *ch = &pdata->channels[i];
327 
328 		switch (ch->mode) {
329 		case mode_temperature:
330 			if (i_temp == GSC_HWMON_MAX_TEMP_CH) {
331 				dev_err(gsc->dev, "too many temp channels\n");
332 				return -EINVAL;
333 			}
334 			hwmon->temp_ch[i_temp] = ch;
335 			hwmon->temp_config[i_temp] = HWMON_T_INPUT |
336 						     HWMON_T_LABEL;
337 			i_temp++;
338 			break;
339 		case mode_voltage:
340 		case mode_voltage_raw:
341 			if (i_in == GSC_HWMON_MAX_IN_CH) {
342 				dev_err(gsc->dev, "too many input channels\n");
343 				return -EINVAL;
344 			}
345 			hwmon->in_ch[i_in] = ch;
346 			hwmon->in_config[i_in] =
347 				HWMON_I_INPUT | HWMON_I_LABEL;
348 			i_in++;
349 			break;
350 		default:
351 			dev_err(gsc->dev, "invalid mode: %d\n", ch->mode);
352 			return -EINVAL;
353 		}
354 	}
355 
356 	/* setup config structures */
357 	hwmon->chip.ops = &gsc_hwmon_ops;
358 	hwmon->chip.info = hwmon->info;
359 	hwmon->info[0] = &hwmon->temp_info;
360 	hwmon->info[1] = &hwmon->in_info;
361 	hwmon->temp_info.type = hwmon_temp;
362 	hwmon->temp_info.config = hwmon->temp_config;
363 	hwmon->in_info.type = hwmon_in;
364 	hwmon->in_info.config = hwmon->in_config;
365 
366 	groups = pdata->fan_base ? gsc_hwmon_groups : NULL;
367 	hwmon_dev = devm_hwmon_device_register_with_info(dev,
368 							 KBUILD_MODNAME, hwmon,
369 							 &hwmon->chip, groups);
370 	return PTR_ERR_OR_ZERO(hwmon_dev);
371 }
372 
373 static const struct of_device_id gsc_hwmon_of_match[] = {
374 	{ .compatible = "gw,gsc-adc", },
375 	{}
376 };
377 
378 static struct platform_driver gsc_hwmon_driver = {
379 	.driver = {
380 		.name = "gsc-hwmon",
381 		.of_match_table = gsc_hwmon_of_match,
382 	},
383 	.probe = gsc_hwmon_probe,
384 };
385 
386 module_platform_driver(gsc_hwmon_driver);
387 
388 MODULE_AUTHOR("Tim Harvey <tharvey@gateworks.com>");
389 MODULE_DESCRIPTION("GSC hardware monitor driver");
390 MODULE_LICENSE("GPL v2");
391