of: MSI: Simplify irqdomain lookup
[linux/fpc-iii.git] / drivers / hwmon / da9052-hwmon.c
blobc9832bfacfe592d76d3c439deb703f91cebc62af
1 /*
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>
27 struct da9052_hwmon {
28 struct da9052 *da9052;
29 struct mutex hwmon_lock;
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_TJUNC] = "BATTERY JUNCTION TEMP",
41 [DA9052_ADC_VBBAT] = "BACK-UP BATTERY VOLTAGE",
44 /* Conversion function for VDDOUT and VBAT */
45 static inline int volt_reg_to_mv(int value)
47 return DIV_ROUND_CLOSEST(value * 2000, 1023) + 2500;
50 /* Conversion function for ADC channels 4, 5 and 6 */
51 static inline int input_reg_to_mv(int value)
53 return DIV_ROUND_CLOSEST(value * 2500, 1023);
56 /* Conversion function for VBBAT */
57 static inline int vbbat_reg_to_mv(int value)
59 return DIV_ROUND_CLOSEST(value * 5000, 1023);
62 static inline int da9052_enable_vddout_channel(struct da9052 *da9052)
64 return da9052_reg_update(da9052, DA9052_ADC_CONT_REG,
65 DA9052_ADCCONT_AUTOVDDEN,
66 DA9052_ADCCONT_AUTOVDDEN);
69 static inline int da9052_disable_vddout_channel(struct da9052 *da9052)
71 return da9052_reg_update(da9052, DA9052_ADC_CONT_REG,
72 DA9052_ADCCONT_AUTOVDDEN, 0);
75 static ssize_t da9052_read_vddout(struct device *dev,
76 struct device_attribute *devattr, char *buf)
78 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
79 int ret, vdd;
81 mutex_lock(&hwmon->hwmon_lock);
83 ret = da9052_enable_vddout_channel(hwmon->da9052);
84 if (ret < 0)
85 goto hwmon_err;
87 vdd = da9052_reg_read(hwmon->da9052, DA9052_VDD_RES_REG);
88 if (vdd < 0) {
89 ret = vdd;
90 goto hwmon_err_release;
93 ret = da9052_disable_vddout_channel(hwmon->da9052);
94 if (ret < 0)
95 goto hwmon_err;
97 mutex_unlock(&hwmon->hwmon_lock);
98 return sprintf(buf, "%d\n", volt_reg_to_mv(vdd));
100 hwmon_err_release:
101 da9052_disable_vddout_channel(hwmon->da9052);
102 hwmon_err:
103 mutex_unlock(&hwmon->hwmon_lock);
104 return ret;
107 static ssize_t da9052_read_ich(struct device *dev,
108 struct device_attribute *devattr, char *buf)
110 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
111 int ret;
113 ret = da9052_reg_read(hwmon->da9052, DA9052_ICHG_AV_REG);
114 if (ret < 0)
115 return ret;
117 /* Equivalent to 3.9mA/bit in register ICHG_AV */
118 return sprintf(buf, "%d\n", DIV_ROUND_CLOSEST(ret * 39, 10));
121 static ssize_t da9052_read_tbat(struct device *dev,
122 struct device_attribute *devattr, char *buf)
124 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
126 return sprintf(buf, "%d\n", da9052_adc_read_temp(hwmon->da9052));
129 static ssize_t da9052_read_vbat(struct device *dev,
130 struct device_attribute *devattr, char *buf)
132 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
133 int ret;
135 ret = da9052_adc_manual_read(hwmon->da9052, DA9052_ADC_VBAT);
136 if (ret < 0)
137 return ret;
139 return sprintf(buf, "%d\n", volt_reg_to_mv(ret));
142 static ssize_t da9052_read_misc_channel(struct device *dev,
143 struct device_attribute *devattr,
144 char *buf)
146 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
147 int channel = to_sensor_dev_attr(devattr)->index;
148 int ret;
150 ret = da9052_adc_manual_read(hwmon->da9052, channel);
151 if (ret < 0)
152 return ret;
154 return sprintf(buf, "%d\n", input_reg_to_mv(ret));
157 static ssize_t da9052_read_tjunc(struct device *dev,
158 struct device_attribute *devattr, char *buf)
160 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
161 int tjunc;
162 int toffset;
164 tjunc = da9052_reg_read(hwmon->da9052, DA9052_TJUNC_RES_REG);
165 if (tjunc < 0)
166 return tjunc;
168 toffset = da9052_reg_read(hwmon->da9052, DA9052_T_OFFSET_REG);
169 if (toffset < 0)
170 return toffset;
173 * Degrees celsius = 1.708 * (TJUNC_RES - T_OFFSET) - 108.8
174 * T_OFFSET is a trim value used to improve accuracy of the result
176 return sprintf(buf, "%d\n", 1708 * (tjunc - toffset) - 108800);
179 static ssize_t da9052_read_vbbat(struct device *dev,
180 struct device_attribute *devattr, char *buf)
182 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
183 int ret;
185 ret = da9052_adc_manual_read(hwmon->da9052, DA9052_ADC_VBBAT);
186 if (ret < 0)
187 return ret;
189 return sprintf(buf, "%d\n", vbbat_reg_to_mv(ret));
192 static ssize_t show_label(struct device *dev,
193 struct device_attribute *devattr, char *buf)
195 return sprintf(buf, "%s\n",
196 input_names[to_sensor_dev_attr(devattr)->index]);
199 static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, da9052_read_vddout, NULL,
200 DA9052_ADC_VDDOUT);
201 static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, show_label, NULL,
202 DA9052_ADC_VDDOUT);
203 static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, da9052_read_vbat, NULL,
204 DA9052_ADC_VBAT);
205 static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL,
206 DA9052_ADC_VBAT);
207 static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, da9052_read_misc_channel, NULL,
208 DA9052_ADC_IN4);
209 static SENSOR_DEVICE_ATTR(in4_label, S_IRUGO, show_label, NULL,
210 DA9052_ADC_IN4);
211 static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, da9052_read_misc_channel, NULL,
212 DA9052_ADC_IN5);
213 static SENSOR_DEVICE_ATTR(in5_label, S_IRUGO, show_label, NULL,
214 DA9052_ADC_IN5);
215 static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, da9052_read_misc_channel, NULL,
216 DA9052_ADC_IN6);
217 static SENSOR_DEVICE_ATTR(in6_label, S_IRUGO, show_label, NULL,
218 DA9052_ADC_IN6);
219 static SENSOR_DEVICE_ATTR(in9_input, S_IRUGO, da9052_read_vbbat, NULL,
220 DA9052_ADC_VBBAT);
221 static SENSOR_DEVICE_ATTR(in9_label, S_IRUGO, show_label, NULL,
222 DA9052_ADC_VBBAT);
224 static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, da9052_read_ich, NULL,
225 DA9052_ADC_ICH);
226 static SENSOR_DEVICE_ATTR(curr1_label, S_IRUGO, show_label, NULL,
227 DA9052_ADC_ICH);
229 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, da9052_read_tbat, NULL,
230 DA9052_ADC_TBAT);
231 static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, show_label, NULL,
232 DA9052_ADC_TBAT);
233 static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO, da9052_read_tjunc, NULL,
234 DA9052_ADC_TJUNC);
235 static SENSOR_DEVICE_ATTR(temp8_label, S_IRUGO, show_label, NULL,
236 DA9052_ADC_TJUNC);
238 static struct attribute *da9052_attrs[] = {
239 &sensor_dev_attr_in0_input.dev_attr.attr,
240 &sensor_dev_attr_in0_label.dev_attr.attr,
241 &sensor_dev_attr_in3_input.dev_attr.attr,
242 &sensor_dev_attr_in3_label.dev_attr.attr,
243 &sensor_dev_attr_in4_input.dev_attr.attr,
244 &sensor_dev_attr_in4_label.dev_attr.attr,
245 &sensor_dev_attr_in5_input.dev_attr.attr,
246 &sensor_dev_attr_in5_label.dev_attr.attr,
247 &sensor_dev_attr_in6_input.dev_attr.attr,
248 &sensor_dev_attr_in6_label.dev_attr.attr,
249 &sensor_dev_attr_in9_input.dev_attr.attr,
250 &sensor_dev_attr_in9_label.dev_attr.attr,
251 &sensor_dev_attr_curr1_input.dev_attr.attr,
252 &sensor_dev_attr_curr1_label.dev_attr.attr,
253 &sensor_dev_attr_temp2_input.dev_attr.attr,
254 &sensor_dev_attr_temp2_label.dev_attr.attr,
255 &sensor_dev_attr_temp8_input.dev_attr.attr,
256 &sensor_dev_attr_temp8_label.dev_attr.attr,
257 NULL
260 ATTRIBUTE_GROUPS(da9052);
262 static int da9052_hwmon_probe(struct platform_device *pdev)
264 struct device *dev = &pdev->dev;
265 struct da9052_hwmon *hwmon;
266 struct device *hwmon_dev;
268 hwmon = devm_kzalloc(dev, sizeof(struct da9052_hwmon), GFP_KERNEL);
269 if (!hwmon)
270 return -ENOMEM;
272 mutex_init(&hwmon->hwmon_lock);
273 hwmon->da9052 = dev_get_drvdata(pdev->dev.parent);
275 hwmon_dev = devm_hwmon_device_register_with_groups(dev, "da9052",
276 hwmon,
277 da9052_groups);
278 return PTR_ERR_OR_ZERO(hwmon_dev);
281 static struct platform_driver da9052_hwmon_driver = {
282 .probe = da9052_hwmon_probe,
283 .driver = {
284 .name = "da9052-hwmon",
288 module_platform_driver(da9052_hwmon_driver);
290 MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
291 MODULE_DESCRIPTION("DA9052 HWMON driver");
292 MODULE_LICENSE("GPL");
293 MODULE_ALIAS("platform:da9052-hwmon");