2 * HWMON Driver for Dialog DA9052
4 * Copyright(c) 2012 Dialog Semiconductor Ltd.
6 * Author: David Dajun Chen <dchen@diasemi.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
15 #include <linux/err.h>
16 #include <linux/hwmon.h>
17 #include <linux/hwmon-sysfs.h>
18 #include <linux/init.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/slab.h>
22 #include <linux/platform_device.h>
24 #include <linux/mfd/da9052/da9052.h>
25 #include <linux/mfd/da9052/reg.h>
28 struct da9052 *da9052;
29 struct device *class_device;
30 struct mutex hwmon_lock;
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_TJUNC] = "BATTERY JUNCTION TEMP",
42 [DA9052_ADC_VBBAT] = "BACK-UP BATTERY VOLTAGE",
45 /* Conversion function for VDDOUT and VBAT */
46 static inline int volt_reg_to_mv(int value)
48 return DIV_ROUND_CLOSEST(value * 1000, 512) + 2500;
51 /* Conversion function for ADC channels 4, 5 and 6 */
52 static inline int input_reg_to_mv(int value)
54 return DIV_ROUND_CLOSEST(value * 2500, 1023);
57 /* Conversion function for VBBAT */
58 static inline int vbbat_reg_to_mv(int value)
60 return DIV_ROUND_CLOSEST(value * 2500, 512);
63 static inline int da9052_enable_vddout_channel(struct da9052 *da9052)
65 return da9052_reg_update(da9052, DA9052_ADC_CONT_REG,
66 DA9052_ADCCONT_AUTOVDDEN,
67 DA9052_ADCCONT_AUTOVDDEN);
70 static inline int da9052_disable_vddout_channel(struct da9052 *da9052)
72 return da9052_reg_update(da9052, DA9052_ADC_CONT_REG,
73 DA9052_ADCCONT_AUTOVDDEN, 0);
76 static ssize_t da9052_read_vddout(struct device *dev,
77 struct device_attribute *devattr, char *buf)
79 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
82 mutex_lock(&hwmon->hwmon_lock);
84 ret = da9052_enable_vddout_channel(hwmon->da9052);
88 vdd = da9052_reg_read(hwmon->da9052, DA9052_VDD_RES_REG);
91 goto hwmon_err_release;
94 ret = da9052_disable_vddout_channel(hwmon->da9052);
98 mutex_unlock(&hwmon->hwmon_lock);
99 return sprintf(buf, "%d\n", volt_reg_to_mv(vdd));
102 da9052_disable_vddout_channel(hwmon->da9052);
104 mutex_unlock(&hwmon->hwmon_lock);
108 static ssize_t da9052_read_ich(struct device *dev,
109 struct device_attribute *devattr, char *buf)
111 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
114 ret = da9052_reg_read(hwmon->da9052, DA9052_ICHG_AV_REG);
118 /* Equivalent to 3.9mA/bit in register ICHG_AV */
119 return sprintf(buf, "%d\n", DIV_ROUND_CLOSEST(ret * 39, 10));
122 static ssize_t da9052_read_tbat(struct device *dev,
123 struct device_attribute *devattr, char *buf)
125 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
127 return sprintf(buf, "%d\n", da9052_adc_read_temp(hwmon->da9052));
130 static ssize_t da9052_read_vbat(struct device *dev,
131 struct device_attribute *devattr, char *buf)
133 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
136 ret = da9052_adc_manual_read(hwmon->da9052, DA9052_ADC_VBAT);
140 return sprintf(buf, "%d\n", volt_reg_to_mv(ret));
143 static ssize_t da9052_read_misc_channel(struct device *dev,
144 struct device_attribute *devattr,
147 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
148 int channel = to_sensor_dev_attr(devattr)->index;
151 ret = da9052_adc_manual_read(hwmon->da9052, channel);
155 return sprintf(buf, "%d\n", input_reg_to_mv(ret));
158 static ssize_t da9052_read_tjunc(struct device *dev,
159 struct device_attribute *devattr, char *buf)
161 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
165 tjunc = da9052_reg_read(hwmon->da9052, DA9052_TJUNC_RES_REG);
169 toffset = da9052_reg_read(hwmon->da9052, DA9052_T_OFFSET_REG);
174 * Degrees celsius = 1.708 * (TJUNC_RES - T_OFFSET) - 108.8
175 * T_OFFSET is a trim value used to improve accuracy of the result
177 return sprintf(buf, "%d\n", 1708 * (tjunc - toffset) - 108800);
180 static ssize_t da9052_read_vbbat(struct device *dev,
181 struct device_attribute *devattr, char *buf)
183 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
186 ret = da9052_adc_manual_read(hwmon->da9052, DA9052_ADC_VBBAT);
190 return sprintf(buf, "%d\n", vbbat_reg_to_mv(ret));
193 static ssize_t da9052_hwmon_show_name(struct device *dev,
194 struct device_attribute *devattr,
197 return sprintf(buf, "da9052-hwmon\n");
200 static ssize_t show_label(struct device *dev,
201 struct device_attribute *devattr, char *buf)
203 return sprintf(buf, "%s\n",
204 input_names[to_sensor_dev_attr(devattr)->index]);
207 static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, da9052_read_vddout, NULL,
209 static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, show_label, NULL,
211 static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, da9052_read_vbat, NULL,
213 static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL,
215 static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, da9052_read_misc_channel, NULL,
217 static SENSOR_DEVICE_ATTR(in4_label, S_IRUGO, show_label, NULL,
219 static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, da9052_read_misc_channel, NULL,
221 static SENSOR_DEVICE_ATTR(in5_label, S_IRUGO, show_label, NULL,
223 static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, da9052_read_misc_channel, NULL,
225 static SENSOR_DEVICE_ATTR(in6_label, S_IRUGO, show_label, NULL,
227 static SENSOR_DEVICE_ATTR(in9_input, S_IRUGO, da9052_read_vbbat, NULL,
229 static SENSOR_DEVICE_ATTR(in9_label, S_IRUGO, show_label, NULL,
232 static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, da9052_read_ich, NULL,
234 static SENSOR_DEVICE_ATTR(curr1_label, S_IRUGO, show_label, NULL,
237 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, da9052_read_tbat, NULL,
239 static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, show_label, NULL,
241 static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO, da9052_read_tjunc, NULL,
243 static SENSOR_DEVICE_ATTR(temp8_label, S_IRUGO, show_label, NULL,
246 static DEVICE_ATTR(name, S_IRUGO, da9052_hwmon_show_name, NULL);
248 static struct attribute *da9052_attr[] = {
250 &sensor_dev_attr_in0_input.dev_attr.attr,
251 &sensor_dev_attr_in0_label.dev_attr.attr,
252 &sensor_dev_attr_in3_input.dev_attr.attr,
253 &sensor_dev_attr_in3_label.dev_attr.attr,
254 &sensor_dev_attr_in4_input.dev_attr.attr,
255 &sensor_dev_attr_in4_label.dev_attr.attr,
256 &sensor_dev_attr_in5_input.dev_attr.attr,
257 &sensor_dev_attr_in5_label.dev_attr.attr,
258 &sensor_dev_attr_in6_input.dev_attr.attr,
259 &sensor_dev_attr_in6_label.dev_attr.attr,
260 &sensor_dev_attr_in9_input.dev_attr.attr,
261 &sensor_dev_attr_in9_label.dev_attr.attr,
262 &sensor_dev_attr_curr1_input.dev_attr.attr,
263 &sensor_dev_attr_curr1_label.dev_attr.attr,
264 &sensor_dev_attr_temp2_input.dev_attr.attr,
265 &sensor_dev_attr_temp2_label.dev_attr.attr,
266 &sensor_dev_attr_temp8_input.dev_attr.attr,
267 &sensor_dev_attr_temp8_label.dev_attr.attr,
271 static const struct attribute_group da9052_attr_group = {.attrs = da9052_attr};
273 static int da9052_hwmon_probe(struct platform_device *pdev)
275 struct da9052_hwmon *hwmon;
278 hwmon = devm_kzalloc(&pdev->dev, sizeof(struct da9052_hwmon),
283 mutex_init(&hwmon->hwmon_lock);
284 hwmon->da9052 = dev_get_drvdata(pdev->dev.parent);
286 platform_set_drvdata(pdev, hwmon);
288 ret = sysfs_create_group(&pdev->dev.kobj, &da9052_attr_group);
292 hwmon->class_device = hwmon_device_register(&pdev->dev);
293 if (IS_ERR(hwmon->class_device)) {
294 ret = PTR_ERR(hwmon->class_device);
301 sysfs_remove_group(&pdev->dev.kobj, &da9052_attr_group);
306 static int da9052_hwmon_remove(struct platform_device *pdev)
308 struct da9052_hwmon *hwmon = platform_get_drvdata(pdev);
310 hwmon_device_unregister(hwmon->class_device);
311 sysfs_remove_group(&pdev->dev.kobj, &da9052_attr_group);
316 static struct platform_driver da9052_hwmon_driver = {
317 .probe = da9052_hwmon_probe,
318 .remove = da9052_hwmon_remove,
320 .name = "da9052-hwmon",
321 .owner = THIS_MODULE,
325 module_platform_driver(da9052_hwmon_driver);
327 MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
328 MODULE_DESCRIPTION("DA9052 HWMON driver");
329 MODULE_LICENSE("GPL");
330 MODULE_ALIAS("platform:da9052-hwmon");