1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * HWMON Driver for Dialog DA9052
5 * Copyright(c) 2012 Dialog Semiconductor Ltd.
7 * Author: David Dajun Chen <dchen@diasemi.com>
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>
20 #include <linux/mfd/da9052/da9052.h>
21 #include <linux/mfd/da9052/reg.h>
22 #include <linux/regulator/consumer.h>
25 struct da9052
*da9052
;
26 struct mutex hwmon_lock
;
29 struct regulator
*tsiref
;
30 struct completion tsidone
;
33 static const char * const input_names
[] = {
34 [DA9052_ADC_VDDOUT
] = "VDDOUT",
35 [DA9052_ADC_ICH
] = "CHARGING CURRENT",
36 [DA9052_ADC_TBAT
] = "BATTERY TEMP",
37 [DA9052_ADC_VBAT
] = "BATTERY VOLTAGE",
38 [DA9052_ADC_IN4
] = "ADC IN4",
39 [DA9052_ADC_IN5
] = "ADC IN5",
40 [DA9052_ADC_IN6
] = "ADC IN6",
41 [DA9052_ADC_TSI_XP
] = "ADC TS X+",
42 [DA9052_ADC_TSI_YP
] = "ADC TS Y+",
43 [DA9052_ADC_TSI_XN
] = "ADC TS X-",
44 [DA9052_ADC_TSI_YN
] = "ADC TS Y-",
45 [DA9052_ADC_TJUNC
] = "BATTERY JUNCTION TEMP",
46 [DA9052_ADC_VBBAT
] = "BACK-UP BATTERY VOLTAGE",
49 /* Conversion function for VDDOUT and VBAT */
50 static inline int volt_reg_to_mv(int value
)
52 return DIV_ROUND_CLOSEST(value
* 2000, 1023) + 2500;
55 /* Conversion function for ADC channels 4, 5 and 6 */
56 static inline int input_reg_to_mv(int value
)
58 return DIV_ROUND_CLOSEST(value
* 2500, 1023);
61 /* Conversion function for VBBAT */
62 static inline int vbbat_reg_to_mv(int value
)
64 return DIV_ROUND_CLOSEST(value
* 5000, 1023);
67 static inline int input_tsireg_to_mv(struct da9052_hwmon
*hwmon
, int value
)
69 return DIV_ROUND_CLOSEST(value
* hwmon
->tsiref_mv
, 1023);
72 static inline int da9052_enable_vddout_channel(struct da9052
*da9052
)
74 return da9052_reg_update(da9052
, DA9052_ADC_CONT_REG
,
75 DA9052_ADCCONT_AUTOVDDEN
,
76 DA9052_ADCCONT_AUTOVDDEN
);
79 static inline int da9052_disable_vddout_channel(struct da9052
*da9052
)
81 return da9052_reg_update(da9052
, DA9052_ADC_CONT_REG
,
82 DA9052_ADCCONT_AUTOVDDEN
, 0);
85 static ssize_t
da9052_vddout_show(struct device
*dev
,
86 struct device_attribute
*devattr
, char *buf
)
88 struct da9052_hwmon
*hwmon
= dev_get_drvdata(dev
);
91 mutex_lock(&hwmon
->hwmon_lock
);
93 ret
= da9052_enable_vddout_channel(hwmon
->da9052
);
97 vdd
= da9052_reg_read(hwmon
->da9052
, DA9052_VDD_RES_REG
);
100 goto hwmon_err_release
;
103 ret
= da9052_disable_vddout_channel(hwmon
->da9052
);
107 mutex_unlock(&hwmon
->hwmon_lock
);
108 return sprintf(buf
, "%d\n", volt_reg_to_mv(vdd
));
111 da9052_disable_vddout_channel(hwmon
->da9052
);
113 mutex_unlock(&hwmon
->hwmon_lock
);
117 static ssize_t
da9052_ich_show(struct device
*dev
,
118 struct device_attribute
*devattr
, char *buf
)
120 struct da9052_hwmon
*hwmon
= dev_get_drvdata(dev
);
123 ret
= da9052_reg_read(hwmon
->da9052
, DA9052_ICHG_AV_REG
);
127 /* Equivalent to 3.9mA/bit in register ICHG_AV */
128 return sprintf(buf
, "%d\n", DIV_ROUND_CLOSEST(ret
* 39, 10));
131 static ssize_t
da9052_tbat_show(struct device
*dev
,
132 struct device_attribute
*devattr
, char *buf
)
134 struct da9052_hwmon
*hwmon
= dev_get_drvdata(dev
);
136 return sprintf(buf
, "%d\n", da9052_adc_read_temp(hwmon
->da9052
));
139 static ssize_t
da9052_vbat_show(struct device
*dev
,
140 struct device_attribute
*devattr
, char *buf
)
142 struct da9052_hwmon
*hwmon
= dev_get_drvdata(dev
);
145 ret
= da9052_adc_manual_read(hwmon
->da9052
, DA9052_ADC_VBAT
);
149 return sprintf(buf
, "%d\n", volt_reg_to_mv(ret
));
152 static ssize_t
da9052_misc_channel_show(struct device
*dev
,
153 struct device_attribute
*devattr
,
156 struct da9052_hwmon
*hwmon
= dev_get_drvdata(dev
);
157 int channel
= to_sensor_dev_attr(devattr
)->index
;
160 ret
= da9052_adc_manual_read(hwmon
->da9052
, channel
);
164 return sprintf(buf
, "%d\n", input_reg_to_mv(ret
));
167 static int da9052_request_tsi_read(struct da9052_hwmon
*hwmon
, int channel
)
169 u8 val
= DA9052_TSICONTB_TSIMAN
;
172 case DA9052_ADC_TSI_XP
:
173 val
|= DA9052_TSICONTB_TSIMUX_XP
;
175 case DA9052_ADC_TSI_YP
:
176 val
|= DA9052_TSICONTB_TSIMUX_YP
;
178 case DA9052_ADC_TSI_XN
:
179 val
|= DA9052_TSICONTB_TSIMUX_XN
;
181 case DA9052_ADC_TSI_YN
:
182 val
|= DA9052_TSICONTB_TSIMUX_YN
;
186 return da9052_reg_write(hwmon
->da9052
, DA9052_TSI_CONT_B_REG
, val
);
189 static int da9052_get_tsi_result(struct da9052_hwmon
*hwmon
, int channel
)
194 /* block read to avoid separation of MSB and LSB */
195 err
= da9052_group_read(hwmon
->da9052
, DA9052_TSI_X_MSB_REG
,
196 ARRAY_SIZE(regs
), regs
);
201 case DA9052_ADC_TSI_XP
:
202 case DA9052_ADC_TSI_XN
:
203 msb
= regs
[0] << DA9052_TSILSB_TSIXL_BITS
;
204 lsb
= regs
[2] & DA9052_TSILSB_TSIXL
;
205 lsb
>>= DA9052_TSILSB_TSIXL_SHIFT
;
207 case DA9052_ADC_TSI_YP
:
208 case DA9052_ADC_TSI_YN
:
209 msb
= regs
[1] << DA9052_TSILSB_TSIYL_BITS
;
210 lsb
= regs
[2] & DA9052_TSILSB_TSIYL
;
211 lsb
>>= DA9052_TSILSB_TSIYL_SHIFT
;
221 static ssize_t
__da9052_read_tsi(struct device
*dev
, int channel
)
223 struct da9052_hwmon
*hwmon
= dev_get_drvdata(dev
);
226 reinit_completion(&hwmon
->tsidone
);
228 ret
= da9052_request_tsi_read(hwmon
, channel
);
232 /* Wait for an conversion done interrupt */
233 if (!wait_for_completion_timeout(&hwmon
->tsidone
,
234 msecs_to_jiffies(500)))
237 return da9052_get_tsi_result(hwmon
, channel
);
240 static ssize_t
da9052_tsi_show(struct device
*dev
,
241 struct device_attribute
*devattr
, char *buf
)
243 struct da9052_hwmon
*hwmon
= dev_get_drvdata(dev
);
244 int channel
= to_sensor_dev_attr(devattr
)->index
;
247 mutex_lock(&hwmon
->hwmon_lock
);
248 ret
= __da9052_read_tsi(dev
, channel
);
249 mutex_unlock(&hwmon
->hwmon_lock
);
254 return sprintf(buf
, "%d\n", input_tsireg_to_mv(hwmon
, ret
));
257 static ssize_t
da9052_tjunc_show(struct device
*dev
,
258 struct device_attribute
*devattr
, char *buf
)
260 struct da9052_hwmon
*hwmon
= dev_get_drvdata(dev
);
264 tjunc
= da9052_reg_read(hwmon
->da9052
, DA9052_TJUNC_RES_REG
);
268 toffset
= da9052_reg_read(hwmon
->da9052
, DA9052_T_OFFSET_REG
);
273 * Degrees celsius = 1.708 * (TJUNC_RES - T_OFFSET) - 108.8
274 * T_OFFSET is a trim value used to improve accuracy of the result
276 return sprintf(buf
, "%d\n", 1708 * (tjunc
- toffset
) - 108800);
279 static ssize_t
da9052_vbbat_show(struct device
*dev
,
280 struct device_attribute
*devattr
, char *buf
)
282 struct da9052_hwmon
*hwmon
= dev_get_drvdata(dev
);
285 ret
= da9052_adc_manual_read(hwmon
->da9052
, DA9052_ADC_VBBAT
);
289 return sprintf(buf
, "%d\n", vbbat_reg_to_mv(ret
));
292 static ssize_t
label_show(struct device
*dev
,
293 struct device_attribute
*devattr
, char *buf
)
295 return sprintf(buf
, "%s\n",
296 input_names
[to_sensor_dev_attr(devattr
)->index
]);
299 static umode_t
da9052_channel_is_visible(struct kobject
*kobj
,
300 struct attribute
*attr
, int index
)
302 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
303 struct da9052_hwmon
*hwmon
= dev_get_drvdata(dev
);
304 struct device_attribute
*dattr
= container_of(attr
,
305 struct device_attribute
, attr
);
306 struct sensor_device_attribute
*sattr
= to_sensor_dev_attr(dattr
);
308 if (!hwmon
->tsi_as_adc
) {
309 switch (sattr
->index
) {
310 case DA9052_ADC_TSI_XP
:
311 case DA9052_ADC_TSI_YP
:
312 case DA9052_ADC_TSI_XN
:
313 case DA9052_ADC_TSI_YN
:
321 static SENSOR_DEVICE_ATTR_RO(in0_input
, da9052_vddout
, DA9052_ADC_VDDOUT
);
322 static SENSOR_DEVICE_ATTR_RO(in0_label
, label
, DA9052_ADC_VDDOUT
);
323 static SENSOR_DEVICE_ATTR_RO(in3_input
, da9052_vbat
, DA9052_ADC_VBAT
);
324 static SENSOR_DEVICE_ATTR_RO(in3_label
, label
, DA9052_ADC_VBAT
);
325 static SENSOR_DEVICE_ATTR_RO(in4_input
, da9052_misc_channel
, DA9052_ADC_IN4
);
326 static SENSOR_DEVICE_ATTR_RO(in4_label
, label
, DA9052_ADC_IN4
);
327 static SENSOR_DEVICE_ATTR_RO(in5_input
, da9052_misc_channel
, DA9052_ADC_IN5
);
328 static SENSOR_DEVICE_ATTR_RO(in5_label
, label
, DA9052_ADC_IN5
);
329 static SENSOR_DEVICE_ATTR_RO(in6_input
, da9052_misc_channel
, DA9052_ADC_IN6
);
330 static SENSOR_DEVICE_ATTR_RO(in6_label
, label
, DA9052_ADC_IN6
);
331 static SENSOR_DEVICE_ATTR_RO(in9_input
, da9052_vbbat
, DA9052_ADC_VBBAT
);
332 static SENSOR_DEVICE_ATTR_RO(in9_label
, label
, DA9052_ADC_VBBAT
);
334 static SENSOR_DEVICE_ATTR_RO(in70_input
, da9052_tsi
, DA9052_ADC_TSI_XP
);
335 static SENSOR_DEVICE_ATTR_RO(in70_label
, label
, DA9052_ADC_TSI_XP
);
336 static SENSOR_DEVICE_ATTR_RO(in71_input
, da9052_tsi
, DA9052_ADC_TSI_XN
);
337 static SENSOR_DEVICE_ATTR_RO(in71_label
, label
, DA9052_ADC_TSI_XN
);
338 static SENSOR_DEVICE_ATTR_RO(in72_input
, da9052_tsi
, DA9052_ADC_TSI_YP
);
339 static SENSOR_DEVICE_ATTR_RO(in72_label
, label
, DA9052_ADC_TSI_YP
);
340 static SENSOR_DEVICE_ATTR_RO(in73_input
, da9052_tsi
, DA9052_ADC_TSI_YN
);
341 static SENSOR_DEVICE_ATTR_RO(in73_label
, label
, DA9052_ADC_TSI_YN
);
343 static SENSOR_DEVICE_ATTR_RO(curr1_input
, da9052_ich
, DA9052_ADC_ICH
);
344 static SENSOR_DEVICE_ATTR_RO(curr1_label
, label
, DA9052_ADC_ICH
);
346 static SENSOR_DEVICE_ATTR_RO(temp2_input
, da9052_tbat
, DA9052_ADC_TBAT
);
347 static SENSOR_DEVICE_ATTR_RO(temp2_label
, label
, DA9052_ADC_TBAT
);
348 static SENSOR_DEVICE_ATTR_RO(temp8_input
, da9052_tjunc
, DA9052_ADC_TJUNC
);
349 static SENSOR_DEVICE_ATTR_RO(temp8_label
, label
, DA9052_ADC_TJUNC
);
351 static struct attribute
*da9052_attrs
[] = {
352 &sensor_dev_attr_in0_input
.dev_attr
.attr
,
353 &sensor_dev_attr_in0_label
.dev_attr
.attr
,
354 &sensor_dev_attr_in3_input
.dev_attr
.attr
,
355 &sensor_dev_attr_in3_label
.dev_attr
.attr
,
356 &sensor_dev_attr_in4_input
.dev_attr
.attr
,
357 &sensor_dev_attr_in4_label
.dev_attr
.attr
,
358 &sensor_dev_attr_in5_input
.dev_attr
.attr
,
359 &sensor_dev_attr_in5_label
.dev_attr
.attr
,
360 &sensor_dev_attr_in6_input
.dev_attr
.attr
,
361 &sensor_dev_attr_in6_label
.dev_attr
.attr
,
362 &sensor_dev_attr_in70_input
.dev_attr
.attr
,
363 &sensor_dev_attr_in70_label
.dev_attr
.attr
,
364 &sensor_dev_attr_in71_input
.dev_attr
.attr
,
365 &sensor_dev_attr_in71_label
.dev_attr
.attr
,
366 &sensor_dev_attr_in72_input
.dev_attr
.attr
,
367 &sensor_dev_attr_in72_label
.dev_attr
.attr
,
368 &sensor_dev_attr_in73_input
.dev_attr
.attr
,
369 &sensor_dev_attr_in73_label
.dev_attr
.attr
,
370 &sensor_dev_attr_in9_input
.dev_attr
.attr
,
371 &sensor_dev_attr_in9_label
.dev_attr
.attr
,
372 &sensor_dev_attr_curr1_input
.dev_attr
.attr
,
373 &sensor_dev_attr_curr1_label
.dev_attr
.attr
,
374 &sensor_dev_attr_temp2_input
.dev_attr
.attr
,
375 &sensor_dev_attr_temp2_label
.dev_attr
.attr
,
376 &sensor_dev_attr_temp8_input
.dev_attr
.attr
,
377 &sensor_dev_attr_temp8_label
.dev_attr
.attr
,
381 static const struct attribute_group da9052_group
= {
382 .attrs
= da9052_attrs
,
383 .is_visible
= da9052_channel_is_visible
,
385 __ATTRIBUTE_GROUPS(da9052
);
387 static irqreturn_t
da9052_tsi_datardy_irq(int irq
, void *data
)
389 struct da9052_hwmon
*hwmon
= data
;
391 complete(&hwmon
->tsidone
);
395 static int da9052_hwmon_probe(struct platform_device
*pdev
)
397 struct device
*dev
= &pdev
->dev
;
398 struct da9052_hwmon
*hwmon
;
399 struct device
*hwmon_dev
;
402 hwmon
= devm_kzalloc(dev
, sizeof(struct da9052_hwmon
), GFP_KERNEL
);
406 platform_set_drvdata(pdev
, hwmon
);
408 mutex_init(&hwmon
->hwmon_lock
);
409 hwmon
->da9052
= dev_get_drvdata(pdev
->dev
.parent
);
411 init_completion(&hwmon
->tsidone
);
414 device_property_read_bool(pdev
->dev
.parent
, "dlg,tsi-as-adc");
416 if (hwmon
->tsi_as_adc
) {
417 hwmon
->tsiref
= devm_regulator_get(pdev
->dev
.parent
, "tsiref");
418 if (IS_ERR(hwmon
->tsiref
)) {
419 err
= PTR_ERR(hwmon
->tsiref
);
420 dev_err(&pdev
->dev
, "failed to get tsiref: %d", err
);
424 err
= regulator_enable(hwmon
->tsiref
);
428 hwmon
->tsiref_mv
= regulator_get_voltage(hwmon
->tsiref
);
429 if (hwmon
->tsiref_mv
< 0) {
430 err
= hwmon
->tsiref_mv
;
434 /* convert from microvolt (DT) to millivolt (hwmon) */
435 hwmon
->tsiref_mv
/= 1000;
437 /* TSIREF limits from datasheet */
438 if (hwmon
->tsiref_mv
< 1800 || hwmon
->tsiref_mv
> 2600) {
439 dev_err(hwmon
->da9052
->dev
, "invalid TSIREF voltage: %d",
445 /* disable touchscreen features */
446 da9052_reg_write(hwmon
->da9052
, DA9052_TSI_CONT_A_REG
, 0x00);
448 /* Sample every 1ms */
449 da9052_reg_update(hwmon
->da9052
, DA9052_ADC_CONT_REG
,
450 DA9052_ADCCONT_ADCMODE
,
451 DA9052_ADCCONT_ADCMODE
);
453 err
= da9052_request_irq(hwmon
->da9052
, DA9052_IRQ_TSIREADY
,
454 "tsiready-irq", da9052_tsi_datardy_irq
,
457 dev_err(&pdev
->dev
, "Failed to register TSIRDY IRQ: %d",
463 hwmon_dev
= devm_hwmon_device_register_with_groups(dev
, "da9052",
466 err
= PTR_ERR_OR_ZERO(hwmon_dev
);
473 if (hwmon
->tsi_as_adc
)
474 da9052_free_irq(hwmon
->da9052
, DA9052_IRQ_TSIREADY
, hwmon
);
477 regulator_disable(hwmon
->tsiref
);
482 static int da9052_hwmon_remove(struct platform_device
*pdev
)
484 struct da9052_hwmon
*hwmon
= platform_get_drvdata(pdev
);
486 if (hwmon
->tsi_as_adc
) {
487 da9052_free_irq(hwmon
->da9052
, DA9052_IRQ_TSIREADY
, hwmon
);
488 regulator_disable(hwmon
->tsiref
);
494 static struct platform_driver da9052_hwmon_driver
= {
495 .probe
= da9052_hwmon_probe
,
496 .remove
= da9052_hwmon_remove
,
498 .name
= "da9052-hwmon",
502 module_platform_driver(da9052_hwmon_driver
);
504 MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
505 MODULE_DESCRIPTION("DA9052 HWMON driver");
506 MODULE_LICENSE("GPL");
507 MODULE_ALIAS("platform:da9052-hwmon");