hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/drivers/hwmon/ad7418.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * An hwmon driver for the Analog Devices AD7416/17/18
34 * Copyright (C) 2006-07 Tower Technologies
....@@ -6,10 +7,6 @@
67 *
78 * Based on lm75.c
89 * Copyright (C) 1998-99 Frodo Looijaard <frodol@dds.nl>
9
- *
10
- * This program is free software; you can redistribute it and/or modify
11
- * it under the terms of the GNU General Public License,
12
- * as published by the Free Software Foundation - version 2.
1310 */
1411
1512 #include <linux/module.h>
....@@ -19,6 +16,7 @@
1916 #include <linux/hwmon-sysfs.h>
2017 #include <linux/err.h>
2118 #include <linux/mutex.h>
19
+#include <linux/of_device.h>
2220 #include <linux/delay.h>
2321 #include <linux/slab.h>
2422
....@@ -54,10 +52,11 @@
5452 u16 in[4];
5553 };
5654
57
-static struct ad7418_data *ad7418_update_device(struct device *dev)
55
+static int ad7418_update_device(struct device *dev)
5856 {
5957 struct ad7418_data *data = dev_get_drvdata(dev);
6058 struct i2c_client *client = data->client;
59
+ s32 val;
6160
6261 mutex_lock(&data->lock);
6362
....@@ -67,63 +66,96 @@
6766 int i, ch;
6867
6968 /* read config register and clear channel bits */
70
- cfg = i2c_smbus_read_byte_data(client, AD7418_REG_CONF);
69
+ val = i2c_smbus_read_byte_data(client, AD7418_REG_CONF);
70
+ if (val < 0)
71
+ goto abort;
72
+
73
+ cfg = val;
7174 cfg &= 0x1F;
7275
73
- i2c_smbus_write_byte_data(client, AD7418_REG_CONF,
76
+ val = i2c_smbus_write_byte_data(client, AD7418_REG_CONF,
7477 cfg | AD7418_CH_TEMP);
78
+ if (val < 0)
79
+ goto abort;
80
+
7581 udelay(30);
7682
7783 for (i = 0; i < 3; i++) {
78
- data->temp[i] =
79
- i2c_smbus_read_word_swapped(client,
80
- AD7418_REG_TEMP[i]);
84
+ val = i2c_smbus_read_word_swapped(client,
85
+ AD7418_REG_TEMP[i]);
86
+ if (val < 0)
87
+ goto abort;
88
+
89
+ data->temp[i] = val;
8190 }
8291
8392 for (i = 0, ch = 4; i < data->adc_max; i++, ch--) {
84
- i2c_smbus_write_byte_data(client,
85
- AD7418_REG_CONF,
93
+ val = i2c_smbus_write_byte_data(client, AD7418_REG_CONF,
8694 cfg | AD7418_REG_ADC_CH(ch));
95
+ if (val < 0)
96
+ goto abort;
8797
8898 udelay(15);
89
- data->in[data->adc_max - 1 - i] =
90
- i2c_smbus_read_word_swapped(client,
91
- AD7418_REG_ADC);
99
+ val = i2c_smbus_read_word_swapped(client,
100
+ AD7418_REG_ADC);
101
+ if (val < 0)
102
+ goto abort;
103
+
104
+ data->in[data->adc_max - 1 - i] = val;
92105 }
93106
94107 /* restore old configuration value */
95
- i2c_smbus_write_word_swapped(client, AD7418_REG_CONF, cfg);
108
+ val = i2c_smbus_write_word_swapped(client, AD7418_REG_CONF,
109
+ cfg);
110
+ if (val < 0)
111
+ goto abort;
96112
97113 data->last_updated = jiffies;
98114 data->valid = 1;
99115 }
100116
101117 mutex_unlock(&data->lock);
118
+ return 0;
102119
103
- return data;
120
+abort:
121
+ data->valid = 0;
122
+ mutex_unlock(&data->lock);
123
+ return val;
104124 }
105125
106
-static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
107
- char *buf)
126
+static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
127
+ char *buf)
108128 {
109129 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
110
- struct ad7418_data *data = ad7418_update_device(dev);
130
+ struct ad7418_data *data = dev_get_drvdata(dev);
131
+ int ret;
132
+
133
+ ret = ad7418_update_device(dev);
134
+ if (ret < 0)
135
+ return ret;
136
+
111137 return sprintf(buf, "%d\n",
112138 LM75_TEMP_FROM_REG(data->temp[attr->index]));
113139 }
114140
115
-static ssize_t show_adc(struct device *dev, struct device_attribute *devattr,
141
+static ssize_t adc_show(struct device *dev, struct device_attribute *devattr,
116142 char *buf)
117143 {
118144 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
119
- struct ad7418_data *data = ad7418_update_device(dev);
145
+ struct ad7418_data *data = dev_get_drvdata(dev);
146
+ int ret;
147
+
148
+ ret = ad7418_update_device(dev);
149
+ if (ret < 0)
150
+ return ret;
120151
121152 return sprintf(buf, "%d\n",
122153 ((data->in[attr->index] >> 6) * 2500 + 512) / 1024);
123154 }
124155
125
-static ssize_t set_temp(struct device *dev, struct device_attribute *devattr,
126
- const char *buf, size_t count)
156
+static ssize_t temp_store(struct device *dev,
157
+ struct device_attribute *devattr, const char *buf,
158
+ size_t count)
127159 {
128160 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
129161 struct ad7418_data *data = dev_get_drvdata(dev);
....@@ -143,16 +175,14 @@
143175 return count;
144176 }
145177
146
-static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
147
-static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
148
- show_temp, set_temp, 1);
149
-static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
150
- show_temp, set_temp, 2);
178
+static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
179
+static SENSOR_DEVICE_ATTR_RW(temp1_max_hyst, temp, 1);
180
+static SENSOR_DEVICE_ATTR_RW(temp1_max, temp, 2);
151181
152
-static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_adc, NULL, 0);
153
-static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_adc, NULL, 1);
154
-static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_adc, NULL, 2);
155
-static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_adc, NULL, 3);
182
+static SENSOR_DEVICE_ATTR_RO(in1_input, adc, 0);
183
+static SENSOR_DEVICE_ATTR_RO(in2_input, adc, 1);
184
+static SENSOR_DEVICE_ATTR_RO(in3_input, adc, 2);
185
+static SENSOR_DEVICE_ATTR_RO(in4_input, adc, 3);
156186
157187 static struct attribute *ad7416_attrs[] = {
158188 &sensor_dev_attr_temp1_max.dev_attr.attr,
....@@ -200,8 +230,9 @@
200230 }
201231 }
202232
203
-static int ad7418_probe(struct i2c_client *client,
204
- const struct i2c_device_id *id)
233
+static const struct i2c_device_id ad7418_id[];
234
+
235
+static int ad7418_probe(struct i2c_client *client)
205236 {
206237 struct device *dev = &client->dev;
207238 struct i2c_adapter *adapter = client->adapter;
....@@ -221,7 +252,10 @@
221252
222253 mutex_init(&data->lock);
223254 data->client = client;
224
- data->type = id->driver_data;
255
+ if (dev->of_node)
256
+ data->type = (enum chips)of_device_get_match_data(dev);
257
+ else
258
+ data->type = i2c_match_id(ad7418_id, client)->driver_data;
225259
226260 switch (data->type) {
227261 case ad7416:
....@@ -259,11 +293,20 @@
259293 };
260294 MODULE_DEVICE_TABLE(i2c, ad7418_id);
261295
296
+static const struct of_device_id ad7418_dt_ids[] = {
297
+ { .compatible = "adi,ad7416", .data = (void *)ad7416, },
298
+ { .compatible = "adi,ad7417", .data = (void *)ad7417, },
299
+ { .compatible = "adi,ad7418", .data = (void *)ad7418, },
300
+ { }
301
+};
302
+MODULE_DEVICE_TABLE(of, ad7418_dt_ids);
303
+
262304 static struct i2c_driver ad7418_driver = {
263305 .driver = {
264306 .name = "ad7418",
307
+ .of_match_table = ad7418_dt_ids,
265308 },
266
- .probe = ad7418_probe,
309
+ .probe_new = ad7418_probe,
267310 .id_table = ad7418_id,
268311 };
269312