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 completion tsidone
;
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",
48 /* Conversion function for VDDOUT and VBAT */
49 static inline int volt_reg_to_mv(int value
)
51 return DIV_ROUND_CLOSEST(value
* 2000, 1023) + 2500;
54 /* Conversion function for ADC channels 4, 5 and 6 */
55 static inline int input_reg_to_mv(int value
)
57 return DIV_ROUND_CLOSEST(value
* 2500, 1023);
60 /* Conversion function for VBBAT */
61 static inline int vbbat_reg_to_mv(int value
)
63 return DIV_ROUND_CLOSEST(value
* 5000, 1023);
66 static inline int input_tsireg_to_mv(struct da9052_hwmon
*hwmon
, int value
)
68 return DIV_ROUND_CLOSEST(value
* hwmon
->tsiref_mv
, 1023);
71 static inline int da9052_enable_vddout_channel(struct da9052
*da9052
)
73 return da9052_reg_update(da9052
, DA9052_ADC_CONT_REG
,
74 DA9052_ADCCONT_AUTOVDDEN
,
75 DA9052_ADCCONT_AUTOVDDEN
);
78 static inline int da9052_disable_vddout_channel(struct da9052
*da9052
)
80 return da9052_reg_update(da9052
, DA9052_ADC_CONT_REG
,
81 DA9052_ADCCONT_AUTOVDDEN
, 0);
84 static ssize_t
da9052_vddout_show(struct device
*dev
,
85 struct device_attribute
*devattr
, char *buf
)
87 struct da9052_hwmon
*hwmon
= dev_get_drvdata(dev
);
90 mutex_lock(&hwmon
->hwmon_lock
);
92 ret
= da9052_enable_vddout_channel(hwmon
->da9052
);
96 vdd
= da9052_reg_read(hwmon
->da9052
, DA9052_VDD_RES_REG
);
99 goto hwmon_err_release
;
102 ret
= da9052_disable_vddout_channel(hwmon
->da9052
);
106 mutex_unlock(&hwmon
->hwmon_lock
);
107 return sprintf(buf
, "%d\n", volt_reg_to_mv(vdd
));
110 da9052_disable_vddout_channel(hwmon
->da9052
);
112 mutex_unlock(&hwmon
->hwmon_lock
);
116 static ssize_t
da9052_ich_show(struct device
*dev
,
117 struct device_attribute
*devattr
, char *buf
)
119 struct da9052_hwmon
*hwmon
= dev_get_drvdata(dev
);
122 ret
= da9052_reg_read(hwmon
->da9052
, DA9052_ICHG_AV_REG
);
126 /* Equivalent to 3.9mA/bit in register ICHG_AV */
127 return sprintf(buf
, "%d\n", DIV_ROUND_CLOSEST(ret
* 39, 10));
130 static ssize_t
da9052_tbat_show(struct device
*dev
,
131 struct device_attribute
*devattr
, char *buf
)
133 struct da9052_hwmon
*hwmon
= dev_get_drvdata(dev
);
135 return sprintf(buf
, "%d\n", da9052_adc_read_temp(hwmon
->da9052
));
138 static ssize_t
da9052_vbat_show(struct device
*dev
,
139 struct device_attribute
*devattr
, char *buf
)
141 struct da9052_hwmon
*hwmon
= dev_get_drvdata(dev
);
144 ret
= da9052_adc_manual_read(hwmon
->da9052
, DA9052_ADC_VBAT
);
148 return sprintf(buf
, "%d\n", volt_reg_to_mv(ret
));
151 static ssize_t
da9052_misc_channel_show(struct device
*dev
,
152 struct device_attribute
*devattr
,
155 struct da9052_hwmon
*hwmon
= dev_get_drvdata(dev
);
156 int channel
= to_sensor_dev_attr(devattr
)->index
;
159 ret
= da9052_adc_manual_read(hwmon
->da9052
, channel
);
163 return sprintf(buf
, "%d\n", input_reg_to_mv(ret
));
166 static int da9052_request_tsi_read(struct da9052_hwmon
*hwmon
, int channel
)
168 u8 val
= DA9052_TSICONTB_TSIMAN
;
171 case DA9052_ADC_TSI_XP
:
172 val
|= DA9052_TSICONTB_TSIMUX_XP
;
174 case DA9052_ADC_TSI_YP
:
175 val
|= DA9052_TSICONTB_TSIMUX_YP
;
177 case DA9052_ADC_TSI_XN
:
178 val
|= DA9052_TSICONTB_TSIMUX_XN
;
180 case DA9052_ADC_TSI_YN
:
181 val
|= DA9052_TSICONTB_TSIMUX_YN
;
185 return da9052_reg_write(hwmon
->da9052
, DA9052_TSI_CONT_B_REG
, val
);
188 static int da9052_get_tsi_result(struct da9052_hwmon
*hwmon
, int channel
)
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
);
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
;
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
;
220 static ssize_t
__da9052_read_tsi(struct device
*dev
, int channel
)
222 struct da9052_hwmon
*hwmon
= dev_get_drvdata(dev
);
225 reinit_completion(&hwmon
->tsidone
);
227 ret
= da9052_request_tsi_read(hwmon
, channel
);
231 /* Wait for an conversion done interrupt */
232 if (!wait_for_completion_timeout(&hwmon
->tsidone
,
233 msecs_to_jiffies(500)))
236 return da9052_get_tsi_result(hwmon
, channel
);
239 static ssize_t
da9052_tsi_show(struct device
*dev
,
240 struct device_attribute
*devattr
, char *buf
)
242 struct da9052_hwmon
*hwmon
= dev_get_drvdata(dev
);
243 int channel
= to_sensor_dev_attr(devattr
)->index
;
246 mutex_lock(&hwmon
->da9052
->auxadc_lock
);
247 ret
= __da9052_read_tsi(dev
, channel
);
248 mutex_unlock(&hwmon
->da9052
->auxadc_lock
);
253 return sprintf(buf
, "%d\n", input_tsireg_to_mv(hwmon
, ret
));
256 static ssize_t
da9052_tjunc_show(struct device
*dev
,
257 struct device_attribute
*devattr
, char *buf
)
259 struct da9052_hwmon
*hwmon
= dev_get_drvdata(dev
);
263 tjunc
= da9052_reg_read(hwmon
->da9052
, DA9052_TJUNC_RES_REG
);
267 toffset
= da9052_reg_read(hwmon
->da9052
, DA9052_T_OFFSET_REG
);
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
275 return sprintf(buf
, "%d\n", 1708 * (tjunc
- toffset
) - 108800);
278 static ssize_t
da9052_vbbat_show(struct device
*dev
,
279 struct device_attribute
*devattr
, char *buf
)
281 struct da9052_hwmon
*hwmon
= dev_get_drvdata(dev
);
284 ret
= da9052_adc_manual_read(hwmon
->da9052
, DA9052_ADC_VBBAT
);
288 return sprintf(buf
, "%d\n", vbbat_reg_to_mv(ret
));
291 static ssize_t
label_show(struct device
*dev
,
292 struct device_attribute
*devattr
, char *buf
)
294 return sprintf(buf
, "%s\n",
295 input_names
[to_sensor_dev_attr(devattr
)->index
]);
298 static umode_t
da9052_channel_is_visible(struct kobject
*kobj
,
299 struct attribute
*attr
, int index
)
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
);
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
:
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
);
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
);
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
);
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
);
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
,
380 static const struct attribute_group da9052_group
= {
381 .attrs
= da9052_attrs
,
382 .is_visible
= da9052_channel_is_visible
,
384 __ATTRIBUTE_GROUPS(da9052
);
386 static irqreturn_t
da9052_tsi_datardy_irq(int irq
, void *data
)
388 struct da9052_hwmon
*hwmon
= data
;
390 complete(&hwmon
->tsidone
);
394 static int da9052_hwmon_probe(struct platform_device
*pdev
)
396 struct device
*dev
= &pdev
->dev
;
397 struct da9052_hwmon
*hwmon
;
398 struct device
*hwmon_dev
;
401 hwmon
= devm_kzalloc(dev
, sizeof(struct da9052_hwmon
), GFP_KERNEL
);
405 platform_set_drvdata(pdev
, hwmon
);
407 mutex_init(&hwmon
->hwmon_lock
);
408 hwmon
->da9052
= dev_get_drvdata(pdev
->dev
.parent
);
410 init_completion(&hwmon
->tsidone
);
413 device_property_read_bool(pdev
->dev
.parent
, "dlg,tsi-as-adc");
415 if (hwmon
->tsi_as_adc
) {
416 tsiref_uv
= devm_regulator_get_enable_read_voltage(dev
->parent
,
419 return dev_err_probe(dev
, tsiref_uv
,
420 "failed to get tsiref voltage\n");
422 /* convert from microvolt (DT) to millivolt (hwmon) */
423 hwmon
->tsiref_mv
= tsiref_uv
/ 1000;
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",
432 /* disable touchscreen features */
433 da9052_reg_write(hwmon
->da9052
, DA9052_TSI_CONT_A_REG
, 0x00);
435 /* Sample every 1ms */
436 da9052_reg_update(hwmon
->da9052
, DA9052_ADC_CONT_REG
,
437 DA9052_ADCCONT_ADCMODE
,
438 DA9052_ADCCONT_ADCMODE
);
440 err
= da9052_request_irq(hwmon
->da9052
, DA9052_IRQ_TSIREADY
,
441 "tsiready-irq", da9052_tsi_datardy_irq
,
444 dev_err(&pdev
->dev
, "Failed to register TSIRDY IRQ: %d",
450 hwmon_dev
= devm_hwmon_device_register_with_groups(dev
, "da9052",
453 err
= PTR_ERR_OR_ZERO(hwmon_dev
);
460 if (hwmon
->tsi_as_adc
)
461 da9052_free_irq(hwmon
->da9052
, DA9052_IRQ_TSIREADY
, hwmon
);
466 static void da9052_hwmon_remove(struct platform_device
*pdev
)
468 struct da9052_hwmon
*hwmon
= platform_get_drvdata(pdev
);
470 if (hwmon
->tsi_as_adc
)
471 da9052_free_irq(hwmon
->da9052
, DA9052_IRQ_TSIREADY
, hwmon
);
474 static struct platform_driver da9052_hwmon_driver
= {
475 .probe
= da9052_hwmon_probe
,
476 .remove
= da9052_hwmon_remove
,
478 .name
= "da9052-hwmon",
482 module_platform_driver(da9052_hwmon_driver
);
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");