1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <linux/module.h>
23#include <linux/jiffies.h>
24#include <linux/i2c.h>
25#include <linux/hwmon.h>
26#include <linux/hwmon-sysfs.h>
27#include <linux/err.h>
28#include <linux/mutex.h>
29#include <linux/sysfs.h>
30#include <linux/slab.h>
31
32
33
34#define AD7414_REG_TEMP 0x00
35#define AD7414_REG_CONF 0x01
36#define AD7414_REG_T_HIGH 0x02
37#define AD7414_REG_T_LOW 0x03
38
39static u8 AD7414_REG_LIMIT[] = { AD7414_REG_T_HIGH, AD7414_REG_T_LOW };
40
41struct ad7414_data {
42 struct device *hwmon_dev;
43 struct mutex lock;
44 char valid;
45 unsigned long next_update;
46 s16 temp_input;
47 s8 temps[ARRAY_SIZE(AD7414_REG_LIMIT)];
48};
49
50
51static inline int ad7414_temp_from_reg(s16 reg)
52{
53
54
55
56
57 return ((int)reg / 64) * 250;
58}
59
60static inline int ad7414_read(struct i2c_client *client, u8 reg)
61{
62 if (reg == AD7414_REG_TEMP)
63 return i2c_smbus_read_word_swapped(client, reg);
64 else
65 return i2c_smbus_read_byte_data(client, reg);
66}
67
68static inline int ad7414_write(struct i2c_client *client, u8 reg, u8 value)
69{
70 return i2c_smbus_write_byte_data(client, reg, value);
71}
72
73static struct ad7414_data *ad7414_update_device(struct device *dev)
74{
75 struct i2c_client *client = to_i2c_client(dev);
76 struct ad7414_data *data = i2c_get_clientdata(client);
77
78 mutex_lock(&data->lock);
79
80 if (time_after(jiffies, data->next_update) || !data->valid) {
81 int value, i;
82
83 dev_dbg(&client->dev, "starting ad7414 update\n");
84
85 value = ad7414_read(client, AD7414_REG_TEMP);
86 if (value < 0)
87 dev_dbg(&client->dev, "AD7414_REG_TEMP err %d\n",
88 value);
89 else
90 data->temp_input = value;
91
92 for (i = 0; i < ARRAY_SIZE(AD7414_REG_LIMIT); ++i) {
93 value = ad7414_read(client, AD7414_REG_LIMIT[i]);
94 if (value < 0)
95 dev_dbg(&client->dev, "AD7414 reg %d err %d\n",
96 AD7414_REG_LIMIT[i], value);
97 else
98 data->temps[i] = value;
99 }
100
101 data->next_update = jiffies + HZ + HZ / 2;
102 data->valid = 1;
103 }
104
105 mutex_unlock(&data->lock);
106
107 return data;
108}
109
110static ssize_t show_temp_input(struct device *dev,
111 struct device_attribute *attr, char *buf)
112{
113 struct ad7414_data *data = ad7414_update_device(dev);
114 return sprintf(buf, "%d\n", ad7414_temp_from_reg(data->temp_input));
115}
116static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL, 0);
117
118static ssize_t show_max_min(struct device *dev, struct device_attribute *attr,
119 char *buf)
120{
121 int index = to_sensor_dev_attr(attr)->index;
122 struct ad7414_data *data = ad7414_update_device(dev);
123 return sprintf(buf, "%d\n", data->temps[index] * 1000);
124}
125
126static ssize_t set_max_min(struct device *dev,
127 struct device_attribute *attr,
128 const char *buf, size_t count)
129{
130 struct i2c_client *client = to_i2c_client(dev);
131 struct ad7414_data *data = i2c_get_clientdata(client);
132 int index = to_sensor_dev_attr(attr)->index;
133 u8 reg = AD7414_REG_LIMIT[index];
134 long temp;
135 int ret = kstrtol(buf, 10, &temp);
136
137 if (ret < 0)
138 return ret;
139
140 temp = SENSORS_LIMIT(temp, -40000, 85000);
141 temp = (temp + (temp < 0 ? -500 : 500)) / 1000;
142
143 mutex_lock(&data->lock);
144 data->temps[index] = temp;
145 ad7414_write(client, reg, temp);
146 mutex_unlock(&data->lock);
147 return count;
148}
149
150static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
151 show_max_min, set_max_min, 0);
152static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO,
153 show_max_min, set_max_min, 1);
154
155static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
156 char *buf)
157{
158 int bitnr = to_sensor_dev_attr(attr)->index;
159 struct ad7414_data *data = ad7414_update_device(dev);
160 int value = (data->temp_input >> bitnr) & 1;
161 return sprintf(buf, "%d\n", value);
162}
163
164static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 3);
165static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 4);
166
167static struct attribute *ad7414_attributes[] = {
168 &sensor_dev_attr_temp1_input.dev_attr.attr,
169 &sensor_dev_attr_temp1_max.dev_attr.attr,
170 &sensor_dev_attr_temp1_min.dev_attr.attr,
171 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
172 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
173 NULL
174};
175
176static const struct attribute_group ad7414_group = {
177 .attrs = ad7414_attributes,
178};
179
180static int ad7414_probe(struct i2c_client *client,
181 const struct i2c_device_id *dev_id)
182{
183 struct ad7414_data *data;
184 int conf;
185 int err;
186
187 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA |
188 I2C_FUNC_SMBUS_READ_WORD_DATA))
189 return -EOPNOTSUPP;
190
191 data = devm_kzalloc(&client->dev, sizeof(struct ad7414_data),
192 GFP_KERNEL);
193 if (!data)
194 return -ENOMEM;
195
196 i2c_set_clientdata(client, data);
197 mutex_init(&data->lock);
198
199 dev_info(&client->dev, "chip found\n");
200
201
202 conf = i2c_smbus_read_byte_data(client, AD7414_REG_CONF);
203 if (conf < 0)
204 dev_warn(&client->dev,
205 "ad7414_probe unable to read config register.\n");
206 else {
207 conf &= ~(1 << 7);
208 i2c_smbus_write_byte_data(client, AD7414_REG_CONF, conf);
209 }
210
211
212 err = sysfs_create_group(&client->dev.kobj, &ad7414_group);
213 if (err)
214 return err;
215
216 data->hwmon_dev = hwmon_device_register(&client->dev);
217 if (IS_ERR(data->hwmon_dev)) {
218 err = PTR_ERR(data->hwmon_dev);
219 goto exit_remove;
220 }
221
222 return 0;
223
224exit_remove:
225 sysfs_remove_group(&client->dev.kobj, &ad7414_group);
226 return err;
227}
228
229static int ad7414_remove(struct i2c_client *client)
230{
231 struct ad7414_data *data = i2c_get_clientdata(client);
232
233 hwmon_device_unregister(data->hwmon_dev);
234 sysfs_remove_group(&client->dev.kobj, &ad7414_group);
235 return 0;
236}
237
238static const struct i2c_device_id ad7414_id[] = {
239 { "ad7414", 0 },
240 {}
241};
242MODULE_DEVICE_TABLE(i2c, ad7414_id);
243
244static struct i2c_driver ad7414_driver = {
245 .driver = {
246 .name = "ad7414",
247 },
248 .probe = ad7414_probe,
249 .remove = ad7414_remove,
250 .id_table = ad7414_id,
251};
252
253module_i2c_driver(ad7414_driver);
254
255MODULE_AUTHOR("Stefan Roese <sr at denx.de>, "
256 "Frank Edelhaeuser <frank.edelhaeuser at spansion.com>");
257
258MODULE_DESCRIPTION("AD7414 driver");
259MODULE_LICENSE("GPL");
260