blob: c9832bfacfe592d76d3c439deb703f91cebc62af [file] [log] [blame]
Ashish Jangame41f6432012-03-17 15:34:41 +05301/*
2 * HWMON Driver for Dialog DA9052
3 *
4 * Copyright(c) 2012 Dialog Semiconductor Ltd.
5 *
6 * Author: David Dajun Chen <dchen@diasemi.com>
7 *
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.
12 *
13 */
14
Ashish Jangame41f6432012-03-17 15:34:41 +053015#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>
23
24#include <linux/mfd/da9052/da9052.h>
25#include <linux/mfd/da9052/reg.h>
26
27struct da9052_hwmon {
28 struct da9052 *da9052;
Ashish Jangame41f6432012-03-17 15:34:41 +053029 struct mutex hwmon_lock;
30};
31
32static 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",
42};
43
44/* Conversion function for VDDOUT and VBAT */
Guenter Roeck088ce2a2013-03-13 16:40:39 -070045static inline int volt_reg_to_mv(int value)
Ashish Jangame41f6432012-03-17 15:34:41 +053046{
Anthony Olechc09088d2013-12-18 15:15:31 +000047 return DIV_ROUND_CLOSEST(value * 2000, 1023) + 2500;
Ashish Jangame41f6432012-03-17 15:34:41 +053048}
49
50/* Conversion function for ADC channels 4, 5 and 6 */
Guenter Roeck088ce2a2013-03-13 16:40:39 -070051static inline int input_reg_to_mv(int value)
Ashish Jangame41f6432012-03-17 15:34:41 +053052{
53 return DIV_ROUND_CLOSEST(value * 2500, 1023);
54}
55
56/* Conversion function for VBBAT */
Guenter Roeck088ce2a2013-03-13 16:40:39 -070057static inline int vbbat_reg_to_mv(int value)
Ashish Jangame41f6432012-03-17 15:34:41 +053058{
Anthony Olechc09088d2013-12-18 15:15:31 +000059 return DIV_ROUND_CLOSEST(value * 5000, 1023);
Ashish Jangame41f6432012-03-17 15:34:41 +053060}
61
Axel Lind9b24e372012-10-11 22:03:51 +080062static inline int da9052_enable_vddout_channel(struct da9052 *da9052)
Ashish Jangame41f6432012-03-17 15:34:41 +053063{
Axel Lind9b24e372012-10-11 22:03:51 +080064 return da9052_reg_update(da9052, DA9052_ADC_CONT_REG,
65 DA9052_ADCCONT_AUTOVDDEN,
66 DA9052_ADCCONT_AUTOVDDEN);
Ashish Jangame41f6432012-03-17 15:34:41 +053067}
68
Axel Lind9b24e372012-10-11 22:03:51 +080069static inline int da9052_disable_vddout_channel(struct da9052 *da9052)
Ashish Jangame41f6432012-03-17 15:34:41 +053070{
Axel Lind9b24e372012-10-11 22:03:51 +080071 return da9052_reg_update(da9052, DA9052_ADC_CONT_REG,
72 DA9052_ADCCONT_AUTOVDDEN, 0);
Ashish Jangame41f6432012-03-17 15:34:41 +053073}
74
75static ssize_t da9052_read_vddout(struct device *dev,
76 struct device_attribute *devattr, char *buf)
77{
78 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
79 int ret, vdd;
80
81 mutex_lock(&hwmon->hwmon_lock);
82
83 ret = da9052_enable_vddout_channel(hwmon->da9052);
84 if (ret < 0)
85 goto hwmon_err;
86
87 vdd = da9052_reg_read(hwmon->da9052, DA9052_VDD_RES_REG);
88 if (vdd < 0) {
89 ret = vdd;
90 goto hwmon_err_release;
91 }
92
93 ret = da9052_disable_vddout_channel(hwmon->da9052);
94 if (ret < 0)
95 goto hwmon_err;
96
97 mutex_unlock(&hwmon->hwmon_lock);
Guenter Roeck088ce2a2013-03-13 16:40:39 -070098 return sprintf(buf, "%d\n", volt_reg_to_mv(vdd));
Ashish Jangame41f6432012-03-17 15:34:41 +053099
100hwmon_err_release:
101 da9052_disable_vddout_channel(hwmon->da9052);
102hwmon_err:
103 mutex_unlock(&hwmon->hwmon_lock);
104 return ret;
105}
106
107static ssize_t da9052_read_ich(struct device *dev,
108 struct device_attribute *devattr, char *buf)
109{
110 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
111 int ret;
112
113 ret = da9052_reg_read(hwmon->da9052, DA9052_ICHG_AV_REG);
114 if (ret < 0)
115 return ret;
116
117 /* Equivalent to 3.9mA/bit in register ICHG_AV */
118 return sprintf(buf, "%d\n", DIV_ROUND_CLOSEST(ret * 39, 10));
119}
120
121static ssize_t da9052_read_tbat(struct device *dev,
122 struct device_attribute *devattr, char *buf)
123{
124 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
125
126 return sprintf(buf, "%d\n", da9052_adc_read_temp(hwmon->da9052));
127}
128
129static ssize_t da9052_read_vbat(struct device *dev,
130 struct device_attribute *devattr, char *buf)
131{
132 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
133 int ret;
134
135 ret = da9052_adc_manual_read(hwmon->da9052, DA9052_ADC_VBAT);
136 if (ret < 0)
137 return ret;
138
Guenter Roeck088ce2a2013-03-13 16:40:39 -0700139 return sprintf(buf, "%d\n", volt_reg_to_mv(ret));
Ashish Jangame41f6432012-03-17 15:34:41 +0530140}
141
142static ssize_t da9052_read_misc_channel(struct device *dev,
143 struct device_attribute *devattr,
144 char *buf)
145{
146 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
147 int channel = to_sensor_dev_attr(devattr)->index;
148 int ret;
149
150 ret = da9052_adc_manual_read(hwmon->da9052, channel);
151 if (ret < 0)
152 return ret;
153
Guenter Roeck088ce2a2013-03-13 16:40:39 -0700154 return sprintf(buf, "%d\n", input_reg_to_mv(ret));
Ashish Jangame41f6432012-03-17 15:34:41 +0530155}
156
157static ssize_t da9052_read_tjunc(struct device *dev,
158 struct device_attribute *devattr, char *buf)
159{
160 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
161 int tjunc;
162 int toffset;
163
164 tjunc = da9052_reg_read(hwmon->da9052, DA9052_TJUNC_RES_REG);
165 if (tjunc < 0)
166 return tjunc;
167
168 toffset = da9052_reg_read(hwmon->da9052, DA9052_T_OFFSET_REG);
169 if (toffset < 0)
170 return toffset;
171
172 /*
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
175 */
176 return sprintf(buf, "%d\n", 1708 * (tjunc - toffset) - 108800);
177}
178
179static ssize_t da9052_read_vbbat(struct device *dev,
180 struct device_attribute *devattr, char *buf)
181{
182 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
183 int ret;
184
185 ret = da9052_adc_manual_read(hwmon->da9052, DA9052_ADC_VBBAT);
186 if (ret < 0)
187 return ret;
188
Guenter Roeck088ce2a2013-03-13 16:40:39 -0700189 return sprintf(buf, "%d\n", vbbat_reg_to_mv(ret));
Ashish Jangame41f6432012-03-17 15:34:41 +0530190}
191
Ashish Jangame41f6432012-03-17 15:34:41 +0530192static ssize_t show_label(struct device *dev,
193 struct device_attribute *devattr, char *buf)
194{
195 return sprintf(buf, "%s\n",
196 input_names[to_sensor_dev_attr(devattr)->index]);
197}
198
199static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, da9052_read_vddout, NULL,
200 DA9052_ADC_VDDOUT);
201static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, show_label, NULL,
202 DA9052_ADC_VDDOUT);
203static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, da9052_read_vbat, NULL,
204 DA9052_ADC_VBAT);
205static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL,
206 DA9052_ADC_VBAT);
207static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, da9052_read_misc_channel, NULL,
208 DA9052_ADC_IN4);
209static SENSOR_DEVICE_ATTR(in4_label, S_IRUGO, show_label, NULL,
210 DA9052_ADC_IN4);
211static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, da9052_read_misc_channel, NULL,
212 DA9052_ADC_IN5);
213static SENSOR_DEVICE_ATTR(in5_label, S_IRUGO, show_label, NULL,
214 DA9052_ADC_IN5);
215static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, da9052_read_misc_channel, NULL,
216 DA9052_ADC_IN6);
217static SENSOR_DEVICE_ATTR(in6_label, S_IRUGO, show_label, NULL,
218 DA9052_ADC_IN6);
219static SENSOR_DEVICE_ATTR(in9_input, S_IRUGO, da9052_read_vbbat, NULL,
220 DA9052_ADC_VBBAT);
221static SENSOR_DEVICE_ATTR(in9_label, S_IRUGO, show_label, NULL,
222 DA9052_ADC_VBBAT);
223
224static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, da9052_read_ich, NULL,
225 DA9052_ADC_ICH);
226static SENSOR_DEVICE_ATTR(curr1_label, S_IRUGO, show_label, NULL,
227 DA9052_ADC_ICH);
228
229static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, da9052_read_tbat, NULL,
230 DA9052_ADC_TBAT);
231static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, show_label, NULL,
232 DA9052_ADC_TBAT);
233static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO, da9052_read_tjunc, NULL,
234 DA9052_ADC_TJUNC);
235static SENSOR_DEVICE_ATTR(temp8_label, S_IRUGO, show_label, NULL,
236 DA9052_ADC_TJUNC);
237
Axel Lin4222eb52014-07-09 09:20:41 +0800238static struct attribute *da9052_attrs[] = {
Ashish Jangame41f6432012-03-17 15:34:41 +0530239 &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
258};
259
Axel Lin4222eb52014-07-09 09:20:41 +0800260ATTRIBUTE_GROUPS(da9052);
Ashish Jangame41f6432012-03-17 15:34:41 +0530261
Bill Pemberton6c931ae2012-11-19 13:22:35 -0500262static int da9052_hwmon_probe(struct platform_device *pdev)
Ashish Jangame41f6432012-03-17 15:34:41 +0530263{
Axel Lin4222eb52014-07-09 09:20:41 +0800264 struct device *dev = &pdev->dev;
Ashish Jangame41f6432012-03-17 15:34:41 +0530265 struct da9052_hwmon *hwmon;
Axel Lin4222eb52014-07-09 09:20:41 +0800266 struct device *hwmon_dev;
Ashish Jangame41f6432012-03-17 15:34:41 +0530267
Axel Lin4222eb52014-07-09 09:20:41 +0800268 hwmon = devm_kzalloc(dev, sizeof(struct da9052_hwmon), GFP_KERNEL);
Ashish Jangame41f6432012-03-17 15:34:41 +0530269 if (!hwmon)
270 return -ENOMEM;
271
272 mutex_init(&hwmon->hwmon_lock);
273 hwmon->da9052 = dev_get_drvdata(pdev->dev.parent);
274
Axel Lin4222eb52014-07-09 09:20:41 +0800275 hwmon_dev = devm_hwmon_device_register_with_groups(dev, "da9052",
276 hwmon,
277 da9052_groups);
278 return PTR_ERR_OR_ZERO(hwmon_dev);
Ashish Jangame41f6432012-03-17 15:34:41 +0530279}
280
281static struct platform_driver da9052_hwmon_driver = {
282 .probe = da9052_hwmon_probe,
Ashish Jangame41f6432012-03-17 15:34:41 +0530283 .driver = {
284 .name = "da9052-hwmon",
Ashish Jangame41f6432012-03-17 15:34:41 +0530285 },
286};
287
288module_platform_driver(da9052_hwmon_driver);
289
290MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
291MODULE_DESCRIPTION("DA9052 HWMON driver");
292MODULE_LICENSE("GPL");
293MODULE_ALIAS("platform:da9052-hwmon");