Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0-or-later
2/* tmp421.c
3 *
4 * Copyright (C) 2009 Andre Prendel <andre.prendel@gmx.de>
5 * Preliminary support by:
6 * Melvin Rook, Raymond Ng
7 */
8
9/*
10 * Driver for the Texas Instruments TMP421 SMBus temperature sensor IC.
11 * Supported models: TMP421, TMP422, TMP423, TMP441, TMP442
12 */
13
14#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/slab.h>
17#include <linux/jiffies.h>
18#include <linux/i2c.h>
19#include <linux/hwmon.h>
20#include <linux/hwmon-sysfs.h>
21#include <linux/err.h>
22#include <linux/of.h>
23#include <linux/sysfs.h>
24
25/* Addresses to scan */
26static const unsigned short normal_i2c[] = { 0x2a, 0x4c, 0x4d, 0x4e, 0x4f,
27 I2C_CLIENT_END };
28
29enum chips { tmp421, tmp422, tmp423, tmp441, tmp442 };
30
31#define MAX_CHANNELS 4
32/* The TMP421 registers */
33#define TMP421_STATUS_REG 0x08
34#define TMP421_CONFIG_REG_1 0x09
35#define TMP421_CONFIG_REG_2 0x0A
36#define TMP421_CONFIG_REG_REN(x) (BIT(3 + (x)))
37#define TMP421_CONFIG_REG_REN_MASK GENMASK(6, 3)
38#define TMP421_CONVERSION_RATE_REG 0x0B
39#define TMP421_N_FACTOR_REG_1 0x21
40#define TMP421_MANUFACTURER_ID_REG 0xFE
41#define TMP421_DEVICE_ID_REG 0xFF
42
43static const u8 TMP421_TEMP_MSB[MAX_CHANNELS] = { 0x00, 0x01, 0x02, 0x03 };
44static const u8 TMP421_TEMP_LSB[MAX_CHANNELS] = { 0x10, 0x11, 0x12, 0x13 };
45
46/* Flags */
47#define TMP421_CONFIG_SHUTDOWN 0x40
48#define TMP421_CONFIG_RANGE 0x04
49
50/* Manufacturer / Device ID's */
51#define TMP421_MANUFACTURER_ID 0x55
52#define TMP421_DEVICE_ID 0x21
53#define TMP422_DEVICE_ID 0x22
54#define TMP423_DEVICE_ID 0x23
55#define TMP441_DEVICE_ID 0x41
56#define TMP442_DEVICE_ID 0x42
57
58static const struct i2c_device_id tmp421_id[] = {
59 { "tmp421", 2 },
60 { "tmp422", 3 },
61 { "tmp423", 4 },
62 { "tmp441", 2 },
63 { "tmp442", 3 },
64 { }
65};
66MODULE_DEVICE_TABLE(i2c, tmp421_id);
67
68static const struct of_device_id __maybe_unused tmp421_of_match[] = {
69 {
70 .compatible = "ti,tmp421",
71 .data = (void *)2
72 },
73 {
74 .compatible = "ti,tmp422",
75 .data = (void *)3
76 },
77 {
78 .compatible = "ti,tmp423",
79 .data = (void *)4
80 },
81 {
82 .compatible = "ti,tmp441",
83 .data = (void *)2
84 },
85 {
86 .compatible = "ti,tmp442",
87 .data = (void *)3
88 },
89 { },
90};
91MODULE_DEVICE_TABLE(of, tmp421_of_match);
92
93struct tmp421_channel {
94 const char *label;
95 bool enabled;
96 s16 temp;
97};
98
99struct tmp421_data {
100 struct i2c_client *client;
101 u32 temp_config[MAX_CHANNELS + 1];
102 struct hwmon_channel_info temp_info;
103 const struct hwmon_channel_info *info[2];
104 struct hwmon_chip_info chip;
105 bool valid;
106 unsigned long last_updated;
107 unsigned long channels;
108 u8 config;
109 struct tmp421_channel channel[MAX_CHANNELS];
110};
111
112static int temp_from_raw(u16 reg, bool extended)
113{
114 /* Mask out status bits */
115 int temp = reg & ~0xf;
116
117 if (extended)
118 temp = temp - 64 * 256;
119 else
120 temp = (s16)temp;
121
122 return DIV_ROUND_CLOSEST(temp * 1000, 256);
123}
124
125static int tmp421_update_device(struct tmp421_data *data)
126{
127 struct i2c_client *client = data->client;
128 int ret = 0;
129 int i;
130
131 if (time_after(jiffies, data->last_updated + (HZ / 2)) ||
132 !data->valid) {
133 data->valid = false;
134 ret = i2c_smbus_read_byte_data(client, TMP421_CONFIG_REG_1);
135 if (ret < 0)
136 return ret;
137 data->config = ret;
138
139 for (i = 0; i < data->channels; i++) {
140 ret = i2c_smbus_read_byte_data(client, TMP421_TEMP_MSB[i]);
141 if (ret < 0)
142 return ret;
143 data->channel[i].temp = ret << 8;
144
145 ret = i2c_smbus_read_byte_data(client, TMP421_TEMP_LSB[i]);
146 if (ret < 0)
147 return ret;
148 data->channel[i].temp |= ret;
149 }
150 data->last_updated = jiffies;
151 data->valid = true;
152 }
153 return 0;
154}
155
156static int tmp421_enable_channels(struct tmp421_data *data)
157{
158 int err;
159 struct i2c_client *client = data->client;
160 struct device *dev = &client->dev;
161 int old = i2c_smbus_read_byte_data(client, TMP421_CONFIG_REG_2);
162 int new, i;
163
164 if (old < 0) {
165 dev_err(dev, "error reading register, can't disable channels\n");
166 return old;
167 }
168
169 new = old & ~TMP421_CONFIG_REG_REN_MASK;
170 for (i = 0; i < data->channels; i++)
171 if (data->channel[i].enabled)
172 new |= TMP421_CONFIG_REG_REN(i);
173
174 if (new == old)
175 return 0;
176
177 err = i2c_smbus_write_byte_data(client, TMP421_CONFIG_REG_2, new);
178 if (err < 0)
179 dev_err(dev, "error writing register, can't disable channels\n");
180
181 return err;
182}
183
184static int tmp421_read(struct device *dev, enum hwmon_sensor_types type,
185 u32 attr, int channel, long *val)
186{
187 struct tmp421_data *tmp421 = dev_get_drvdata(dev);
188 int ret = 0;
189
190 ret = tmp421_update_device(tmp421);
191 if (ret)
192 return ret;
193
194 switch (attr) {
195 case hwmon_temp_input:
196 if (!tmp421->channel[channel].enabled)
197 return -ENODATA;
198 *val = temp_from_raw(tmp421->channel[channel].temp,
199 tmp421->config & TMP421_CONFIG_RANGE);
200 return 0;
201 case hwmon_temp_fault:
202 if (!tmp421->channel[channel].enabled)
203 return -ENODATA;
204 /*
205 * Any of OPEN or /PVLD bits indicate a hardware mulfunction
206 * and the conversion result may be incorrect
207 */
208 *val = !!(tmp421->channel[channel].temp & 0x03);
209 return 0;
210 case hwmon_temp_enable:
211 *val = tmp421->channel[channel].enabled;
212 return 0;
213 default:
214 return -EOPNOTSUPP;
215 }
216
217}
218
219static int tmp421_read_string(struct device *dev, enum hwmon_sensor_types type,
220 u32 attr, int channel, const char **str)
221{
222 struct tmp421_data *data = dev_get_drvdata(dev);
223
224 *str = data->channel[channel].label;
225
226 return 0;
227}
228
229static int tmp421_write(struct device *dev, enum hwmon_sensor_types type,
230 u32 attr, int channel, long val)
231{
232 struct tmp421_data *data = dev_get_drvdata(dev);
233 int ret;
234
235 switch (attr) {
236 case hwmon_temp_enable:
237 data->channel[channel].enabled = val;
238 ret = tmp421_enable_channels(data);
239 break;
240 default:
241 ret = -EOPNOTSUPP;
242 }
243
244 return ret;
245}
246
247static umode_t tmp421_is_visible(const void *data, enum hwmon_sensor_types type,
248 u32 attr, int channel)
249{
250 switch (attr) {
251 case hwmon_temp_fault:
252 case hwmon_temp_input:
253 case hwmon_temp_label:
254 return 0444;
255 case hwmon_temp_enable:
256 return 0644;
257 default:
258 return 0;
259 }
260}
261
262static int tmp421_init_client(struct tmp421_data *data)
263{
264 int config, config_orig;
265 struct i2c_client *client = data->client;
266
267 /* Set the conversion rate to 2 Hz */
268 i2c_smbus_write_byte_data(client, TMP421_CONVERSION_RATE_REG, 0x05);
269
270 /* Start conversions (disable shutdown if necessary) */
271 config = i2c_smbus_read_byte_data(client, TMP421_CONFIG_REG_1);
272 if (config < 0) {
273 dev_err(&client->dev,
274 "Could not read configuration register (%d)\n", config);
275 return config;
276 }
277
278 config_orig = config;
279 config &= ~TMP421_CONFIG_SHUTDOWN;
280
281 if (config != config_orig) {
282 dev_info(&client->dev, "Enable monitoring chip\n");
283 i2c_smbus_write_byte_data(client, TMP421_CONFIG_REG_1, config);
284 }
285
286 return tmp421_enable_channels(data);
287}
288
289static int tmp421_detect(struct i2c_client *client,
290 struct i2c_board_info *info)
291{
292 enum chips kind;
293 struct i2c_adapter *adapter = client->adapter;
294 static const char * const names[] = {
295 "TMP421", "TMP422", "TMP423",
296 "TMP441", "TMP442"
297 };
298 int addr = client->addr;
299 u8 reg;
300
301 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
302 return -ENODEV;
303
304 reg = i2c_smbus_read_byte_data(client, TMP421_MANUFACTURER_ID_REG);
305 if (reg != TMP421_MANUFACTURER_ID)
306 return -ENODEV;
307
308 reg = i2c_smbus_read_byte_data(client, TMP421_CONVERSION_RATE_REG);
309 if (reg & 0xf8)
310 return -ENODEV;
311
312 reg = i2c_smbus_read_byte_data(client, TMP421_STATUS_REG);
313 if (reg & 0x7f)
314 return -ENODEV;
315
316 reg = i2c_smbus_read_byte_data(client, TMP421_DEVICE_ID_REG);
317 switch (reg) {
318 case TMP421_DEVICE_ID:
319 kind = tmp421;
320 break;
321 case TMP422_DEVICE_ID:
322 if (addr == 0x2a)
323 return -ENODEV;
324 kind = tmp422;
325 break;
326 case TMP423_DEVICE_ID:
327 if (addr != 0x4c && addr != 0x4d)
328 return -ENODEV;
329 kind = tmp423;
330 break;
331 case TMP441_DEVICE_ID:
332 kind = tmp441;
333 break;
334 case TMP442_DEVICE_ID:
335 if (addr != 0x4c && addr != 0x4d)
336 return -ENODEV;
337 kind = tmp442;
338 break;
339 default:
340 return -ENODEV;
341 }
342
343 strscpy(info->type, tmp421_id[kind].name, I2C_NAME_SIZE);
344 dev_info(&adapter->dev, "Detected TI %s chip at 0x%02x\n",
345 names[kind], client->addr);
346
347 return 0;
348}
349
350static int tmp421_probe_child_from_dt(struct i2c_client *client,
351 struct device_node *child,
352 struct tmp421_data *data)
353
354{
355 struct device *dev = &client->dev;
356 u32 i;
357 s32 val;
358 int err;
359
360 err = of_property_read_u32(child, "reg", &i);
361 if (err) {
362 dev_err(dev, "missing reg property of %pOFn\n", child);
363 return err;
364 }
365
366 if (i >= data->channels) {
367 dev_err(dev, "invalid reg %d of %pOFn\n", i, child);
368 return -EINVAL;
369 }
370
371 err = of_property_read_string(child, "label", &data->channel[i].label);
372 if (err == -ENODATA || err == -EILSEQ) {
373 dev_err(dev, "invalid label property in %pOFn\n", child);
374 return err;
375 }
376 if (data->channel[i].label)
377 data->temp_config[i] |= HWMON_T_LABEL;
378
379 data->channel[i].enabled = of_device_is_available(child);
380
381 err = of_property_read_s32(child, "ti,n-factor", &val);
382 if (!err) {
383 if (i == 0) {
384 dev_err(dev, "n-factor can't be set for internal channel\n");
385 return -EINVAL;
386 }
387
388 if (val > 127 || val < -128) {
389 dev_err(dev, "n-factor for channel %d invalid (%d)\n",
390 i, val);
391 return -EINVAL;
392 }
393 i2c_smbus_write_byte_data(client, TMP421_N_FACTOR_REG_1 + i - 1,
394 val);
395 }
396
397 return 0;
398}
399
400static int tmp421_probe_from_dt(struct i2c_client *client, struct tmp421_data *data)
401{
402 struct device *dev = &client->dev;
403 const struct device_node *np = dev->of_node;
404 int err;
405
406 for_each_child_of_node_scoped(np, child) {
407 if (strcmp(child->name, "channel"))
408 continue;
409
410 err = tmp421_probe_child_from_dt(client, child, data);
411 if (err)
412 return err;
413 }
414
415 return 0;
416}
417
418static const struct hwmon_ops tmp421_ops = {
419 .is_visible = tmp421_is_visible,
420 .read = tmp421_read,
421 .read_string = tmp421_read_string,
422 .write = tmp421_write,
423};
424
425static int tmp421_probe(struct i2c_client *client)
426{
427 struct device *dev = &client->dev;
428 struct device *hwmon_dev;
429 struct tmp421_data *data;
430 int i, err;
431
432 data = devm_kzalloc(dev, sizeof(struct tmp421_data), GFP_KERNEL);
433 if (!data)
434 return -ENOMEM;
435
436 data->channels = (unsigned long)i2c_get_match_data(client);
437 data->client = client;
438
439 for (i = 0; i < data->channels; i++) {
440 data->temp_config[i] = HWMON_T_INPUT | HWMON_T_FAULT | HWMON_T_ENABLE;
441 data->channel[i].enabled = true;
442 }
443
444 err = tmp421_probe_from_dt(client, data);
445 if (err)
446 return err;
447
448 err = tmp421_init_client(data);
449 if (err)
450 return err;
451
452 data->chip.ops = &tmp421_ops;
453 data->chip.info = data->info;
454
455 data->info[0] = &data->temp_info;
456
457 data->temp_info.type = hwmon_temp;
458 data->temp_info.config = data->temp_config;
459
460 hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
461 data,
462 &data->chip,
463 NULL);
464 return PTR_ERR_OR_ZERO(hwmon_dev);
465}
466
467static struct i2c_driver tmp421_driver = {
468 .class = I2C_CLASS_HWMON,
469 .driver = {
470 .name = "tmp421",
471 .of_match_table = of_match_ptr(tmp421_of_match),
472 },
473 .probe = tmp421_probe,
474 .id_table = tmp421_id,
475 .detect = tmp421_detect,
476 .address_list = normal_i2c,
477};
478
479module_i2c_driver(tmp421_driver);
480
481MODULE_AUTHOR("Andre Prendel <andre.prendel@gmx.de>");
482MODULE_DESCRIPTION("Texas Instruments TMP421/422/423/441/442 temperature sensor driver");
483MODULE_LICENSE("GPL");