.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* |
---|
2 | 3 | * An hwmon driver for the Analog Devices AD7416/17/18 |
---|
3 | 4 | * Copyright (C) 2006-07 Tower Technologies |
---|
.. | .. |
---|
6 | 7 | * |
---|
7 | 8 | * Based on lm75.c |
---|
8 | 9 | * 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. |
---|
13 | 10 | */ |
---|
14 | 11 | |
---|
15 | 12 | #include <linux/module.h> |
---|
.. | .. |
---|
19 | 16 | #include <linux/hwmon-sysfs.h> |
---|
20 | 17 | #include <linux/err.h> |
---|
21 | 18 | #include <linux/mutex.h> |
---|
| 19 | +#include <linux/of_device.h> |
---|
22 | 20 | #include <linux/delay.h> |
---|
23 | 21 | #include <linux/slab.h> |
---|
24 | 22 | |
---|
.. | .. |
---|
54 | 52 | u16 in[4]; |
---|
55 | 53 | }; |
---|
56 | 54 | |
---|
57 | | -static struct ad7418_data *ad7418_update_device(struct device *dev) |
---|
| 55 | +static int ad7418_update_device(struct device *dev) |
---|
58 | 56 | { |
---|
59 | 57 | struct ad7418_data *data = dev_get_drvdata(dev); |
---|
60 | 58 | struct i2c_client *client = data->client; |
---|
| 59 | + s32 val; |
---|
61 | 60 | |
---|
62 | 61 | mutex_lock(&data->lock); |
---|
63 | 62 | |
---|
.. | .. |
---|
67 | 66 | int i, ch; |
---|
68 | 67 | |
---|
69 | 68 | /* 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; |
---|
71 | 74 | cfg &= 0x1F; |
---|
72 | 75 | |
---|
73 | | - i2c_smbus_write_byte_data(client, AD7418_REG_CONF, |
---|
| 76 | + val = i2c_smbus_write_byte_data(client, AD7418_REG_CONF, |
---|
74 | 77 | cfg | AD7418_CH_TEMP); |
---|
| 78 | + if (val < 0) |
---|
| 79 | + goto abort; |
---|
| 80 | + |
---|
75 | 81 | udelay(30); |
---|
76 | 82 | |
---|
77 | 83 | 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; |
---|
81 | 90 | } |
---|
82 | 91 | |
---|
83 | 92 | 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, |
---|
86 | 94 | cfg | AD7418_REG_ADC_CH(ch)); |
---|
| 95 | + if (val < 0) |
---|
| 96 | + goto abort; |
---|
87 | 97 | |
---|
88 | 98 | 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; |
---|
92 | 105 | } |
---|
93 | 106 | |
---|
94 | 107 | /* 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; |
---|
96 | 112 | |
---|
97 | 113 | data->last_updated = jiffies; |
---|
98 | 114 | data->valid = 1; |
---|
99 | 115 | } |
---|
100 | 116 | |
---|
101 | 117 | mutex_unlock(&data->lock); |
---|
| 118 | + return 0; |
---|
102 | 119 | |
---|
103 | | - return data; |
---|
| 120 | +abort: |
---|
| 121 | + data->valid = 0; |
---|
| 122 | + mutex_unlock(&data->lock); |
---|
| 123 | + return val; |
---|
104 | 124 | } |
---|
105 | 125 | |
---|
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) |
---|
108 | 128 | { |
---|
109 | 129 | 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 | + |
---|
111 | 137 | return sprintf(buf, "%d\n", |
---|
112 | 138 | LM75_TEMP_FROM_REG(data->temp[attr->index])); |
---|
113 | 139 | } |
---|
114 | 140 | |
---|
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, |
---|
116 | 142 | char *buf) |
---|
117 | 143 | { |
---|
118 | 144 | 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; |
---|
120 | 151 | |
---|
121 | 152 | return sprintf(buf, "%d\n", |
---|
122 | 153 | ((data->in[attr->index] >> 6) * 2500 + 512) / 1024); |
---|
123 | 154 | } |
---|
124 | 155 | |
---|
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) |
---|
127 | 159 | { |
---|
128 | 160 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
---|
129 | 161 | struct ad7418_data *data = dev_get_drvdata(dev); |
---|
.. | .. |
---|
143 | 175 | return count; |
---|
144 | 176 | } |
---|
145 | 177 | |
---|
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); |
---|
151 | 181 | |
---|
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); |
---|
156 | 186 | |
---|
157 | 187 | static struct attribute *ad7416_attrs[] = { |
---|
158 | 188 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
---|
.. | .. |
---|
200 | 230 | } |
---|
201 | 231 | } |
---|
202 | 232 | |
---|
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) |
---|
205 | 236 | { |
---|
206 | 237 | struct device *dev = &client->dev; |
---|
207 | 238 | struct i2c_adapter *adapter = client->adapter; |
---|
.. | .. |
---|
221 | 252 | |
---|
222 | 253 | mutex_init(&data->lock); |
---|
223 | 254 | 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; |
---|
225 | 259 | |
---|
226 | 260 | switch (data->type) { |
---|
227 | 261 | case ad7416: |
---|
.. | .. |
---|
259 | 293 | }; |
---|
260 | 294 | MODULE_DEVICE_TABLE(i2c, ad7418_id); |
---|
261 | 295 | |
---|
| 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 | + |
---|
262 | 304 | static struct i2c_driver ad7418_driver = { |
---|
263 | 305 | .driver = { |
---|
264 | 306 | .name = "ad7418", |
---|
| 307 | + .of_match_table = ad7418_dt_ids, |
---|
265 | 308 | }, |
---|
266 | | - .probe = ad7418_probe, |
---|
| 309 | + .probe_new = ad7418_probe, |
---|
267 | 310 | .id_table = ad7418_id, |
---|
268 | 311 | }; |
---|
269 | 312 | |
---|