usb: dwc3: rockchip: fix possible circular deadlock
[firefly-linux-kernel-4.4.55.git] / drivers / hwmon / adt7411.c
1 /*
2  *  Driver for the ADT7411 (I2C/SPI 8 channel 10 bit ADC & temperature-sensor)
3  *
4  *  Copyright (C) 2008, 2010 Pengutronix
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License version 2 as
8  *  published by the Free Software Foundation.
9  *
10  *  TODO: SPI, support for external temperature sensor
11  *        use power-down mode for suspend?, interrupt handling?
12  */
13
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/err.h>
18 #include <linux/mutex.h>
19 #include <linux/jiffies.h>
20 #include <linux/i2c.h>
21 #include <linux/hwmon.h>
22 #include <linux/hwmon-sysfs.h>
23 #include <linux/slab.h>
24
25 #define ADT7411_REG_INT_TEMP_VDD_LSB            0x03
26 #define ADT7411_REG_EXT_TEMP_AIN14_LSB          0x04
27 #define ADT7411_REG_VDD_MSB                     0x06
28 #define ADT7411_REG_INT_TEMP_MSB                0x07
29 #define ADT7411_REG_EXT_TEMP_AIN1_MSB           0x08
30
31 #define ADT7411_REG_CFG1                        0x18
32 #define ADT7411_CFG1_START_MONITOR              (1 << 0)
33 #define ADT7411_CFG1_RESERVED_BIT3              (1 << 3)
34
35 #define ADT7411_REG_CFG2                        0x19
36 #define ADT7411_CFG2_DISABLE_AVG                (1 << 5)
37
38 #define ADT7411_REG_CFG3                        0x1a
39 #define ADT7411_CFG3_ADC_CLK_225                (1 << 0)
40 #define ADT7411_CFG3_REF_VDD                    (1 << 4)
41
42 #define ADT7411_REG_DEVICE_ID                   0x4d
43 #define ADT7411_REG_MANUFACTURER_ID             0x4e
44
45 #define ADT7411_DEVICE_ID                       0x2
46 #define ADT7411_MANUFACTURER_ID                 0x41
47
48 static const unsigned short normal_i2c[] = { 0x48, 0x4a, 0x4b, I2C_CLIENT_END };
49
50 struct adt7411_data {
51         struct mutex device_lock;       /* for "atomic" device accesses */
52         struct mutex update_lock;
53         unsigned long next_update;
54         int vref_cached;
55         struct i2c_client *client;
56 };
57
58 /*
59  * When reading a register containing (up to 4) lsb, all associated
60  * msb-registers get locked by the hardware. After _one_ of those msb is read,
61  * _all_ are unlocked. In order to use this locking correctly, reading lsb/msb
62  * is protected here with a mutex, too.
63  */
64 static int adt7411_read_10_bit(struct i2c_client *client, u8 lsb_reg,
65                                 u8 msb_reg, u8 lsb_shift)
66 {
67         struct adt7411_data *data = i2c_get_clientdata(client);
68         int val, tmp;
69
70         mutex_lock(&data->device_lock);
71
72         val = i2c_smbus_read_byte_data(client, lsb_reg);
73         if (val < 0)
74                 goto exit_unlock;
75
76         tmp = (val >> lsb_shift) & 3;
77         val = i2c_smbus_read_byte_data(client, msb_reg);
78
79         if (val >= 0)
80                 val = (val << 2) | tmp;
81
82  exit_unlock:
83         mutex_unlock(&data->device_lock);
84
85         return val;
86 }
87
88 static int adt7411_modify_bit(struct i2c_client *client, u8 reg, u8 bit,
89                                 bool flag)
90 {
91         struct adt7411_data *data = i2c_get_clientdata(client);
92         int ret, val;
93
94         mutex_lock(&data->device_lock);
95
96         ret = i2c_smbus_read_byte_data(client, reg);
97         if (ret < 0)
98                 goto exit_unlock;
99
100         if (flag)
101                 val = ret | bit;
102         else
103                 val = ret & ~bit;
104
105         ret = i2c_smbus_write_byte_data(client, reg, val);
106
107  exit_unlock:
108         mutex_unlock(&data->device_lock);
109         return ret;
110 }
111
112 static ssize_t adt7411_show_vdd(struct device *dev,
113                                 struct device_attribute *attr, char *buf)
114 {
115         struct adt7411_data *data = dev_get_drvdata(dev);
116         struct i2c_client *client = data->client;
117         int ret = adt7411_read_10_bit(client, ADT7411_REG_INT_TEMP_VDD_LSB,
118                         ADT7411_REG_VDD_MSB, 2);
119
120         return ret < 0 ? ret : sprintf(buf, "%u\n", ret * 7000 / 1024);
121 }
122
123 static ssize_t adt7411_show_temp(struct device *dev,
124                         struct device_attribute *attr, char *buf)
125 {
126         struct adt7411_data *data = dev_get_drvdata(dev);
127         struct i2c_client *client = data->client;
128         int val = adt7411_read_10_bit(client, ADT7411_REG_INT_TEMP_VDD_LSB,
129                         ADT7411_REG_INT_TEMP_MSB, 0);
130
131         if (val < 0)
132                 return val;
133
134         val = val & 0x200 ? val - 0x400 : val; /* 10 bit signed */
135
136         return sprintf(buf, "%d\n", val * 250);
137 }
138
139 static ssize_t adt7411_show_input(struct device *dev,
140                                   struct device_attribute *attr, char *buf)
141 {
142         int nr = to_sensor_dev_attr(attr)->index;
143         struct adt7411_data *data = dev_get_drvdata(dev);
144         struct i2c_client *client = data->client;
145         int val;
146         u8 lsb_reg, lsb_shift;
147
148         mutex_lock(&data->update_lock);
149         if (time_after_eq(jiffies, data->next_update)) {
150                 val = i2c_smbus_read_byte_data(client, ADT7411_REG_CFG3);
151                 if (val < 0)
152                         goto exit_unlock;
153
154                 if (val & ADT7411_CFG3_REF_VDD) {
155                         val = adt7411_read_10_bit(client,
156                                         ADT7411_REG_INT_TEMP_VDD_LSB,
157                                         ADT7411_REG_VDD_MSB, 2);
158                         if (val < 0)
159                                 goto exit_unlock;
160
161                         data->vref_cached = val * 7000 / 1024;
162                 } else {
163                         data->vref_cached = 2250;
164                 }
165
166                 data->next_update = jiffies + HZ;
167         }
168
169         lsb_reg = ADT7411_REG_EXT_TEMP_AIN14_LSB + (nr >> 2);
170         lsb_shift = 2 * (nr & 0x03);
171         val = adt7411_read_10_bit(client, lsb_reg,
172                         ADT7411_REG_EXT_TEMP_AIN1_MSB + nr, lsb_shift);
173         if (val < 0)
174                 goto exit_unlock;
175
176         val = sprintf(buf, "%u\n", val * data->vref_cached / 1024);
177  exit_unlock:
178         mutex_unlock(&data->update_lock);
179         return val;
180 }
181
182 static ssize_t adt7411_show_bit(struct device *dev,
183                                 struct device_attribute *attr, char *buf)
184 {
185         struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(attr);
186         struct adt7411_data *data = dev_get_drvdata(dev);
187         struct i2c_client *client = data->client;
188         int ret = i2c_smbus_read_byte_data(client, attr2->index);
189
190         return ret < 0 ? ret : sprintf(buf, "%u\n", !!(ret & attr2->nr));
191 }
192
193 static ssize_t adt7411_set_bit(struct device *dev,
194                                struct device_attribute *attr, const char *buf,
195                                size_t count)
196 {
197         struct sensor_device_attribute_2 *s_attr2 = to_sensor_dev_attr_2(attr);
198         struct adt7411_data *data = dev_get_drvdata(dev);
199         struct i2c_client *client = data->client;
200         int ret;
201         unsigned long flag;
202
203         ret = kstrtoul(buf, 0, &flag);
204         if (ret || flag > 1)
205                 return -EINVAL;
206
207         ret = adt7411_modify_bit(client, s_attr2->index, s_attr2->nr, flag);
208
209         /* force update */
210         mutex_lock(&data->update_lock);
211         data->next_update = jiffies;
212         mutex_unlock(&data->update_lock);
213
214         return ret < 0 ? ret : count;
215 }
216
217 #define ADT7411_BIT_ATTR(__name, __reg, __bit) \
218         SENSOR_DEVICE_ATTR_2(__name, S_IRUGO | S_IWUSR, adt7411_show_bit, \
219         adt7411_set_bit, __bit, __reg)
220
221 static DEVICE_ATTR(temp1_input, S_IRUGO, adt7411_show_temp, NULL);
222 static DEVICE_ATTR(in0_input, S_IRUGO, adt7411_show_vdd, NULL);
223 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, adt7411_show_input, NULL, 0);
224 static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, adt7411_show_input, NULL, 1);
225 static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, adt7411_show_input, NULL, 2);
226 static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, adt7411_show_input, NULL, 3);
227 static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, adt7411_show_input, NULL, 4);
228 static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, adt7411_show_input, NULL, 5);
229 static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, adt7411_show_input, NULL, 6);
230 static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, adt7411_show_input, NULL, 7);
231 static ADT7411_BIT_ATTR(no_average, ADT7411_REG_CFG2, ADT7411_CFG2_DISABLE_AVG);
232 static ADT7411_BIT_ATTR(fast_sampling, ADT7411_REG_CFG3, ADT7411_CFG3_ADC_CLK_225);
233 static ADT7411_BIT_ATTR(adc_ref_vdd, ADT7411_REG_CFG3, ADT7411_CFG3_REF_VDD);
234
235 static struct attribute *adt7411_attrs[] = {
236         &dev_attr_temp1_input.attr,
237         &dev_attr_in0_input.attr,
238         &sensor_dev_attr_in1_input.dev_attr.attr,
239         &sensor_dev_attr_in2_input.dev_attr.attr,
240         &sensor_dev_attr_in3_input.dev_attr.attr,
241         &sensor_dev_attr_in4_input.dev_attr.attr,
242         &sensor_dev_attr_in5_input.dev_attr.attr,
243         &sensor_dev_attr_in6_input.dev_attr.attr,
244         &sensor_dev_attr_in7_input.dev_attr.attr,
245         &sensor_dev_attr_in8_input.dev_attr.attr,
246         &sensor_dev_attr_no_average.dev_attr.attr,
247         &sensor_dev_attr_fast_sampling.dev_attr.attr,
248         &sensor_dev_attr_adc_ref_vdd.dev_attr.attr,
249         NULL
250 };
251
252 ATTRIBUTE_GROUPS(adt7411);
253
254 static int adt7411_detect(struct i2c_client *client,
255                           struct i2c_board_info *info)
256 {
257         int val;
258
259         if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
260                 return -ENODEV;
261
262         val = i2c_smbus_read_byte_data(client, ADT7411_REG_MANUFACTURER_ID);
263         if (val < 0 || val != ADT7411_MANUFACTURER_ID) {
264                 dev_dbg(&client->dev,
265                         "Wrong manufacturer ID. Got %d, expected %d\n",
266                         val, ADT7411_MANUFACTURER_ID);
267                 return -ENODEV;
268         }
269
270         val = i2c_smbus_read_byte_data(client, ADT7411_REG_DEVICE_ID);
271         if (val < 0 || val != ADT7411_DEVICE_ID) {
272                 dev_dbg(&client->dev,
273                         "Wrong device ID. Got %d, expected %d\n",
274                         val, ADT7411_DEVICE_ID);
275                 return -ENODEV;
276         }
277
278         strlcpy(info->type, "adt7411", I2C_NAME_SIZE);
279
280         return 0;
281 }
282
283 static int adt7411_probe(struct i2c_client *client,
284                                    const struct i2c_device_id *id)
285 {
286         struct device *dev = &client->dev;
287         struct adt7411_data *data;
288         struct device *hwmon_dev;
289         int ret;
290
291         data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
292         if (!data)
293                 return -ENOMEM;
294
295         i2c_set_clientdata(client, data);
296         data->client = client;
297         mutex_init(&data->device_lock);
298         mutex_init(&data->update_lock);
299
300         /* According to the datasheet, we must only write 1 to bit 3 */
301         ret = adt7411_modify_bit(client, ADT7411_REG_CFG1,
302                                  ADT7411_CFG1_RESERVED_BIT3
303                                  | ADT7411_CFG1_START_MONITOR, 1);
304         if (ret < 0)
305                 return ret;
306
307         /* force update on first occasion */
308         data->next_update = jiffies;
309
310         hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
311                                                            data,
312                                                            adt7411_groups);
313         return PTR_ERR_OR_ZERO(hwmon_dev);
314 }
315
316 static const struct i2c_device_id adt7411_id[] = {
317         { "adt7411", 0 },
318         { }
319 };
320 MODULE_DEVICE_TABLE(i2c, adt7411_id);
321
322 static struct i2c_driver adt7411_driver = {
323         .driver         = {
324                 .name           = "adt7411",
325         },
326         .probe  = adt7411_probe,
327         .id_table = adt7411_id,
328         .detect = adt7411_detect,
329         .address_list = normal_i2c,
330         .class = I2C_CLASS_HWMON,
331 };
332
333 module_i2c_driver(adt7411_driver);
334
335 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de> and "
336         "Wolfram Sang <w.sang@pengutronix.de>");
337 MODULE_DESCRIPTION("ADT7411 driver");
338 MODULE_LICENSE("GPL v2");