1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * HWMON Driver for Dialog DA9052
4 *
5 * Copyright(c) 2012 Dialog Semiconductor Ltd.
6 *
7 * Author: David Dajun Chen <dchen@diasemi.com>
8 */
9
10 #include <linux/err.h>
11 #include <linux/hwmon.h>
12 #include <linux/hwmon-sysfs.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/platform_device.h>
18 #include <linux/property.h>
19
20 #include <linux/mfd/da9052/da9052.h>
21 #include <linux/mfd/da9052/reg.h>
22 #include <linux/regulator/consumer.h>
23
24 struct da9052_hwmon {
25 struct da9052 *da9052;
26 struct mutex hwmon_lock;
27 bool tsi_as_adc;
28 int tsiref_mv;
29 struct completion tsidone;
30 };
31
32 static const char * const input_names[] = {
33 [DA9052_ADC_VDDOUT] = "VDDOUT",
34 [DA9052_ADC_ICH] = "CHARGING CURRENT",
35 [DA9052_ADC_TBAT] = "BATTERY TEMP",
36 [DA9052_ADC_VBAT] = "BATTERY VOLTAGE",
37 [DA9052_ADC_IN4] = "ADC IN4",
38 [DA9052_ADC_IN5] = "ADC IN5",
39 [DA9052_ADC_IN6] = "ADC IN6",
40 [DA9052_ADC_TSI_XP] = "ADC TS X+",
41 [DA9052_ADC_TSI_YP] = "ADC TS Y+",
42 [DA9052_ADC_TSI_XN] = "ADC TS X-",
43 [DA9052_ADC_TSI_YN] = "ADC TS Y-",
44 [DA9052_ADC_TJUNC] = "BATTERY JUNCTION TEMP",
45 [DA9052_ADC_VBBAT] = "BACK-UP BATTERY VOLTAGE",
46 };
47
48 /* Conversion function for VDDOUT and VBAT */
volt_reg_to_mv(int value)49 static inline int volt_reg_to_mv(int value)
50 {
51 return DIV_ROUND_CLOSEST(value * 2000, 1023) + 2500;
52 }
53
54 /* Conversion function for ADC channels 4, 5 and 6 */
input_reg_to_mv(int value)55 static inline int input_reg_to_mv(int value)
56 {
57 return DIV_ROUND_CLOSEST(value * 2500, 1023);
58 }
59
60 /* Conversion function for VBBAT */
vbbat_reg_to_mv(int value)61 static inline int vbbat_reg_to_mv(int value)
62 {
63 return DIV_ROUND_CLOSEST(value * 5000, 1023);
64 }
65
input_tsireg_to_mv(struct da9052_hwmon * hwmon,int value)66 static inline int input_tsireg_to_mv(struct da9052_hwmon *hwmon, int value)
67 {
68 return DIV_ROUND_CLOSEST(value * hwmon->tsiref_mv, 1023);
69 }
70
da9052_enable_vddout_channel(struct da9052 * da9052)71 static inline int da9052_enable_vddout_channel(struct da9052 *da9052)
72 {
73 return da9052_reg_update(da9052, DA9052_ADC_CONT_REG,
74 DA9052_ADCCONT_AUTOVDDEN,
75 DA9052_ADCCONT_AUTOVDDEN);
76 }
77
da9052_disable_vddout_channel(struct da9052 * da9052)78 static inline int da9052_disable_vddout_channel(struct da9052 *da9052)
79 {
80 return da9052_reg_update(da9052, DA9052_ADC_CONT_REG,
81 DA9052_ADCCONT_AUTOVDDEN, 0);
82 }
83
da9052_vddout_show(struct device * dev,struct device_attribute * devattr,char * buf)84 static ssize_t da9052_vddout_show(struct device *dev,
85 struct device_attribute *devattr, char *buf)
86 {
87 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
88 int ret, vdd;
89
90 mutex_lock(&hwmon->hwmon_lock);
91
92 ret = da9052_enable_vddout_channel(hwmon->da9052);
93 if (ret < 0)
94 goto hwmon_err;
95
96 vdd = da9052_reg_read(hwmon->da9052, DA9052_VDD_RES_REG);
97 if (vdd < 0) {
98 ret = vdd;
99 goto hwmon_err_release;
100 }
101
102 ret = da9052_disable_vddout_channel(hwmon->da9052);
103 if (ret < 0)
104 goto hwmon_err;
105
106 mutex_unlock(&hwmon->hwmon_lock);
107 return sprintf(buf, "%d\n", volt_reg_to_mv(vdd));
108
109 hwmon_err_release:
110 da9052_disable_vddout_channel(hwmon->da9052);
111 hwmon_err:
112 mutex_unlock(&hwmon->hwmon_lock);
113 return ret;
114 }
115
da9052_ich_show(struct device * dev,struct device_attribute * devattr,char * buf)116 static ssize_t da9052_ich_show(struct device *dev,
117 struct device_attribute *devattr, char *buf)
118 {
119 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
120 int ret;
121
122 ret = da9052_reg_read(hwmon->da9052, DA9052_ICHG_AV_REG);
123 if (ret < 0)
124 return ret;
125
126 /* Equivalent to 3.9mA/bit in register ICHG_AV */
127 return sprintf(buf, "%d\n", DIV_ROUND_CLOSEST(ret * 39, 10));
128 }
129
da9052_tbat_show(struct device * dev,struct device_attribute * devattr,char * buf)130 static ssize_t da9052_tbat_show(struct device *dev,
131 struct device_attribute *devattr, char *buf)
132 {
133 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
134
135 return sprintf(buf, "%d\n", da9052_adc_read_temp(hwmon->da9052));
136 }
137
da9052_vbat_show(struct device * dev,struct device_attribute * devattr,char * buf)138 static ssize_t da9052_vbat_show(struct device *dev,
139 struct device_attribute *devattr, char *buf)
140 {
141 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
142 int ret;
143
144 ret = da9052_adc_manual_read(hwmon->da9052, DA9052_ADC_VBAT);
145 if (ret < 0)
146 return ret;
147
148 return sprintf(buf, "%d\n", volt_reg_to_mv(ret));
149 }
150
da9052_misc_channel_show(struct device * dev,struct device_attribute * devattr,char * buf)151 static ssize_t da9052_misc_channel_show(struct device *dev,
152 struct device_attribute *devattr,
153 char *buf)
154 {
155 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
156 int channel = to_sensor_dev_attr(devattr)->index;
157 int ret;
158
159 ret = da9052_adc_manual_read(hwmon->da9052, channel);
160 if (ret < 0)
161 return ret;
162
163 return sprintf(buf, "%d\n", input_reg_to_mv(ret));
164 }
165
da9052_request_tsi_read(struct da9052_hwmon * hwmon,int channel)166 static int da9052_request_tsi_read(struct da9052_hwmon *hwmon, int channel)
167 {
168 u8 val = DA9052_TSICONTB_TSIMAN;
169
170 switch (channel) {
171 case DA9052_ADC_TSI_XP:
172 val |= DA9052_TSICONTB_TSIMUX_XP;
173 break;
174 case DA9052_ADC_TSI_YP:
175 val |= DA9052_TSICONTB_TSIMUX_YP;
176 break;
177 case DA9052_ADC_TSI_XN:
178 val |= DA9052_TSICONTB_TSIMUX_XN;
179 break;
180 case DA9052_ADC_TSI_YN:
181 val |= DA9052_TSICONTB_TSIMUX_YN;
182 break;
183 }
184
185 return da9052_reg_write(hwmon->da9052, DA9052_TSI_CONT_B_REG, val);
186 }
187
da9052_get_tsi_result(struct da9052_hwmon * hwmon,int channel)188 static int da9052_get_tsi_result(struct da9052_hwmon *hwmon, int channel)
189 {
190 u8 regs[3];
191 int msb, lsb, err;
192
193 /* block read to avoid separation of MSB and LSB */
194 err = da9052_group_read(hwmon->da9052, DA9052_TSI_X_MSB_REG,
195 ARRAY_SIZE(regs), regs);
196 if (err)
197 return err;
198
199 switch (channel) {
200 case DA9052_ADC_TSI_XP:
201 case DA9052_ADC_TSI_XN:
202 msb = regs[0] << DA9052_TSILSB_TSIXL_BITS;
203 lsb = regs[2] & DA9052_TSILSB_TSIXL;
204 lsb >>= DA9052_TSILSB_TSIXL_SHIFT;
205 break;
206 case DA9052_ADC_TSI_YP:
207 case DA9052_ADC_TSI_YN:
208 msb = regs[1] << DA9052_TSILSB_TSIYL_BITS;
209 lsb = regs[2] & DA9052_TSILSB_TSIYL;
210 lsb >>= DA9052_TSILSB_TSIYL_SHIFT;
211 break;
212 default:
213 return -EINVAL;
214 }
215
216 return msb | lsb;
217 }
218
219
__da9052_read_tsi(struct device * dev,int channel)220 static ssize_t __da9052_read_tsi(struct device *dev, int channel)
221 {
222 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
223 int ret;
224
225 reinit_completion(&hwmon->tsidone);
226
227 ret = da9052_request_tsi_read(hwmon, channel);
228 if (ret < 0)
229 return ret;
230
231 /* Wait for an conversion done interrupt */
232 if (!wait_for_completion_timeout(&hwmon->tsidone,
233 msecs_to_jiffies(500)))
234 return -ETIMEDOUT;
235
236 return da9052_get_tsi_result(hwmon, channel);
237 }
238
da9052_tsi_show(struct device * dev,struct device_attribute * devattr,char * buf)239 static ssize_t da9052_tsi_show(struct device *dev,
240 struct device_attribute *devattr, char *buf)
241 {
242 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
243 int channel = to_sensor_dev_attr(devattr)->index;
244 int ret;
245
246 mutex_lock(&hwmon->da9052->auxadc_lock);
247 ret = __da9052_read_tsi(dev, channel);
248 mutex_unlock(&hwmon->da9052->auxadc_lock);
249
250 if (ret < 0)
251 return ret;
252 else
253 return sprintf(buf, "%d\n", input_tsireg_to_mv(hwmon, ret));
254 }
255
da9052_tjunc_show(struct device * dev,struct device_attribute * devattr,char * buf)256 static ssize_t da9052_tjunc_show(struct device *dev,
257 struct device_attribute *devattr, char *buf)
258 {
259 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
260 int tjunc;
261 int toffset;
262
263 tjunc = da9052_reg_read(hwmon->da9052, DA9052_TJUNC_RES_REG);
264 if (tjunc < 0)
265 return tjunc;
266
267 toffset = da9052_reg_read(hwmon->da9052, DA9052_T_OFFSET_REG);
268 if (toffset < 0)
269 return toffset;
270
271 /*
272 * Degrees celsius = 1.708 * (TJUNC_RES - T_OFFSET) - 108.8
273 * T_OFFSET is a trim value used to improve accuracy of the result
274 */
275 return sprintf(buf, "%d\n", 1708 * (tjunc - toffset) - 108800);
276 }
277
da9052_vbbat_show(struct device * dev,struct device_attribute * devattr,char * buf)278 static ssize_t da9052_vbbat_show(struct device *dev,
279 struct device_attribute *devattr, char *buf)
280 {
281 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
282 int ret;
283
284 ret = da9052_adc_manual_read(hwmon->da9052, DA9052_ADC_VBBAT);
285 if (ret < 0)
286 return ret;
287
288 return sprintf(buf, "%d\n", vbbat_reg_to_mv(ret));
289 }
290
label_show(struct device * dev,struct device_attribute * devattr,char * buf)291 static ssize_t label_show(struct device *dev,
292 struct device_attribute *devattr, char *buf)
293 {
294 return sprintf(buf, "%s\n",
295 input_names[to_sensor_dev_attr(devattr)->index]);
296 }
297
da9052_channel_is_visible(struct kobject * kobj,struct attribute * attr,int index)298 static umode_t da9052_channel_is_visible(struct kobject *kobj,
299 struct attribute *attr, int index)
300 {
301 struct device *dev = kobj_to_dev(kobj);
302 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
303 struct device_attribute *dattr = container_of(attr,
304 struct device_attribute, attr);
305 struct sensor_device_attribute *sattr = to_sensor_dev_attr(dattr);
306
307 if (!hwmon->tsi_as_adc) {
308 switch (sattr->index) {
309 case DA9052_ADC_TSI_XP:
310 case DA9052_ADC_TSI_YP:
311 case DA9052_ADC_TSI_XN:
312 case DA9052_ADC_TSI_YN:
313 return 0;
314 }
315 }
316
317 return attr->mode;
318 }
319
320 static SENSOR_DEVICE_ATTR_RO(in0_input, da9052_vddout, DA9052_ADC_VDDOUT);
321 static SENSOR_DEVICE_ATTR_RO(in0_label, label, DA9052_ADC_VDDOUT);
322 static SENSOR_DEVICE_ATTR_RO(in3_input, da9052_vbat, DA9052_ADC_VBAT);
323 static SENSOR_DEVICE_ATTR_RO(in3_label, label, DA9052_ADC_VBAT);
324 static SENSOR_DEVICE_ATTR_RO(in4_input, da9052_misc_channel, DA9052_ADC_IN4);
325 static SENSOR_DEVICE_ATTR_RO(in4_label, label, DA9052_ADC_IN4);
326 static SENSOR_DEVICE_ATTR_RO(in5_input, da9052_misc_channel, DA9052_ADC_IN5);
327 static SENSOR_DEVICE_ATTR_RO(in5_label, label, DA9052_ADC_IN5);
328 static SENSOR_DEVICE_ATTR_RO(in6_input, da9052_misc_channel, DA9052_ADC_IN6);
329 static SENSOR_DEVICE_ATTR_RO(in6_label, label, DA9052_ADC_IN6);
330 static SENSOR_DEVICE_ATTR_RO(in9_input, da9052_vbbat, DA9052_ADC_VBBAT);
331 static SENSOR_DEVICE_ATTR_RO(in9_label, label, DA9052_ADC_VBBAT);
332
333 static SENSOR_DEVICE_ATTR_RO(in70_input, da9052_tsi, DA9052_ADC_TSI_XP);
334 static SENSOR_DEVICE_ATTR_RO(in70_label, label, DA9052_ADC_TSI_XP);
335 static SENSOR_DEVICE_ATTR_RO(in71_input, da9052_tsi, DA9052_ADC_TSI_XN);
336 static SENSOR_DEVICE_ATTR_RO(in71_label, label, DA9052_ADC_TSI_XN);
337 static SENSOR_DEVICE_ATTR_RO(in72_input, da9052_tsi, DA9052_ADC_TSI_YP);
338 static SENSOR_DEVICE_ATTR_RO(in72_label, label, DA9052_ADC_TSI_YP);
339 static SENSOR_DEVICE_ATTR_RO(in73_input, da9052_tsi, DA9052_ADC_TSI_YN);
340 static SENSOR_DEVICE_ATTR_RO(in73_label, label, DA9052_ADC_TSI_YN);
341
342 static SENSOR_DEVICE_ATTR_RO(curr1_input, da9052_ich, DA9052_ADC_ICH);
343 static SENSOR_DEVICE_ATTR_RO(curr1_label, label, DA9052_ADC_ICH);
344
345 static SENSOR_DEVICE_ATTR_RO(temp2_input, da9052_tbat, DA9052_ADC_TBAT);
346 static SENSOR_DEVICE_ATTR_RO(temp2_label, label, DA9052_ADC_TBAT);
347 static SENSOR_DEVICE_ATTR_RO(temp8_input, da9052_tjunc, DA9052_ADC_TJUNC);
348 static SENSOR_DEVICE_ATTR_RO(temp8_label, label, DA9052_ADC_TJUNC);
349
350 static struct attribute *da9052_attrs[] = {
351 &sensor_dev_attr_in0_input.dev_attr.attr,
352 &sensor_dev_attr_in0_label.dev_attr.attr,
353 &sensor_dev_attr_in3_input.dev_attr.attr,
354 &sensor_dev_attr_in3_label.dev_attr.attr,
355 &sensor_dev_attr_in4_input.dev_attr.attr,
356 &sensor_dev_attr_in4_label.dev_attr.attr,
357 &sensor_dev_attr_in5_input.dev_attr.attr,
358 &sensor_dev_attr_in5_label.dev_attr.attr,
359 &sensor_dev_attr_in6_input.dev_attr.attr,
360 &sensor_dev_attr_in6_label.dev_attr.attr,
361 &sensor_dev_attr_in70_input.dev_attr.attr,
362 &sensor_dev_attr_in70_label.dev_attr.attr,
363 &sensor_dev_attr_in71_input.dev_attr.attr,
364 &sensor_dev_attr_in71_label.dev_attr.attr,
365 &sensor_dev_attr_in72_input.dev_attr.attr,
366 &sensor_dev_attr_in72_label.dev_attr.attr,
367 &sensor_dev_attr_in73_input.dev_attr.attr,
368 &sensor_dev_attr_in73_label.dev_attr.attr,
369 &sensor_dev_attr_in9_input.dev_attr.attr,
370 &sensor_dev_attr_in9_label.dev_attr.attr,
371 &sensor_dev_attr_curr1_input.dev_attr.attr,
372 &sensor_dev_attr_curr1_label.dev_attr.attr,
373 &sensor_dev_attr_temp2_input.dev_attr.attr,
374 &sensor_dev_attr_temp2_label.dev_attr.attr,
375 &sensor_dev_attr_temp8_input.dev_attr.attr,
376 &sensor_dev_attr_temp8_label.dev_attr.attr,
377 NULL
378 };
379
380 static const struct attribute_group da9052_group = {
381 .attrs = da9052_attrs,
382 .is_visible = da9052_channel_is_visible,
383 };
384 __ATTRIBUTE_GROUPS(da9052);
385
da9052_tsi_datardy_irq(int irq,void * data)386 static irqreturn_t da9052_tsi_datardy_irq(int irq, void *data)
387 {
388 struct da9052_hwmon *hwmon = data;
389
390 complete(&hwmon->tsidone);
391 return IRQ_HANDLED;
392 }
393
da9052_hwmon_probe(struct platform_device * pdev)394 static int da9052_hwmon_probe(struct platform_device *pdev)
395 {
396 struct device *dev = &pdev->dev;
397 struct da9052_hwmon *hwmon;
398 struct device *hwmon_dev;
399 int err, tsiref_uv;
400
401 hwmon = devm_kzalloc(dev, sizeof(struct da9052_hwmon), GFP_KERNEL);
402 if (!hwmon)
403 return -ENOMEM;
404
405 platform_set_drvdata(pdev, hwmon);
406
407 mutex_init(&hwmon->hwmon_lock);
408 hwmon->da9052 = dev_get_drvdata(pdev->dev.parent);
409
410 init_completion(&hwmon->tsidone);
411
412 hwmon->tsi_as_adc =
413 device_property_read_bool(pdev->dev.parent, "dlg,tsi-as-adc");
414
415 if (hwmon->tsi_as_adc) {
416 tsiref_uv = devm_regulator_get_enable_read_voltage(dev->parent,
417 "tsiref");
418 if (tsiref_uv < 0)
419 return dev_err_probe(dev, tsiref_uv,
420 "failed to get tsiref voltage\n");
421
422 /* convert from microvolt (DT) to millivolt (hwmon) */
423 hwmon->tsiref_mv = tsiref_uv / 1000;
424
425 /* TSIREF limits from datasheet */
426 if (hwmon->tsiref_mv < 1800 || hwmon->tsiref_mv > 2600) {
427 dev_err(hwmon->da9052->dev, "invalid TSIREF voltage: %d",
428 hwmon->tsiref_mv);
429 return -ENXIO;
430 }
431
432 /* disable touchscreen features */
433 da9052_reg_write(hwmon->da9052, DA9052_TSI_CONT_A_REG, 0x00);
434
435 /* Sample every 1ms */
436 da9052_reg_update(hwmon->da9052, DA9052_ADC_CONT_REG,
437 DA9052_ADCCONT_ADCMODE,
438 DA9052_ADCCONT_ADCMODE);
439
440 err = da9052_request_irq(hwmon->da9052, DA9052_IRQ_TSIREADY,
441 "tsiready-irq", da9052_tsi_datardy_irq,
442 hwmon);
443 if (err) {
444 dev_err(&pdev->dev, "Failed to register TSIRDY IRQ: %d",
445 err);
446 return err;
447 }
448 }
449
450 hwmon_dev = devm_hwmon_device_register_with_groups(dev, "da9052",
451 hwmon,
452 da9052_groups);
453 err = PTR_ERR_OR_ZERO(hwmon_dev);
454 if (err)
455 goto exit_irq;
456
457 return 0;
458
459 exit_irq:
460 if (hwmon->tsi_as_adc)
461 da9052_free_irq(hwmon->da9052, DA9052_IRQ_TSIREADY, hwmon);
462
463 return err;
464 }
465
da9052_hwmon_remove(struct platform_device * pdev)466 static void da9052_hwmon_remove(struct platform_device *pdev)
467 {
468 struct da9052_hwmon *hwmon = platform_get_drvdata(pdev);
469
470 if (hwmon->tsi_as_adc)
471 da9052_free_irq(hwmon->da9052, DA9052_IRQ_TSIREADY, hwmon);
472 }
473
474 static struct platform_driver da9052_hwmon_driver = {
475 .probe = da9052_hwmon_probe,
476 .remove_new = da9052_hwmon_remove,
477 .driver = {
478 .name = "da9052-hwmon",
479 },
480 };
481
482 module_platform_driver(da9052_hwmon_driver);
483
484 MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
485 MODULE_DESCRIPTION("DA9052 HWMON driver");
486 MODULE_LICENSE("GPL");
487 MODULE_ALIAS("platform:da9052-hwmon");
488