2 * An hwmon driver for the Microchip TC74
4 * Copyright 2015 Maciej Szmigiero <mail@maciej.szmigiero.name>
7 * Copyright 2006 Stefan Roese, DENX Software Engineering
8 * Copyright 2008 Sean MacLennan, PIKA Technologies
9 * Copyright 2008 Frank Edelhaeuser, Spansion Inc.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
17 #include <linux/bitops.h>
18 #include <linux/err.h>
19 #include <linux/hwmon.h>
20 #include <linux/hwmon-sysfs.h>
21 #include <linux/i2c.h>
22 #include <linux/jiffies.h>
23 #include <linux/module.h>
24 #include <linux/mutex.h>
25 #include <linux/slab.h>
26 #include <linux/sysfs.h>
29 #define TC74_REG_TEMP 0x00
30 #define TC74_REG_CONFIG 0x01
33 struct i2c_client
*client
;
34 struct mutex lock
; /* atomic read data updates */
35 bool valid
; /* validity of fields below */
36 unsigned long next_update
; /* In jiffies */
37 s8 temp_input
; /* Temp value in dC */
40 static int tc74_update_device(struct device
*dev
)
42 struct tc74_data
*data
= dev_get_drvdata(dev
);
43 struct i2c_client
*client
= data
->client
;
46 ret
= mutex_lock_interruptible(&data
->lock
);
50 if (time_after(jiffies
, data
->next_update
) || !data
->valid
) {
53 value
= i2c_smbus_read_byte_data(client
, TC74_REG_CONFIG
);
55 dev_dbg(&client
->dev
, "TC74_REG_CONFIG read err %d\n",
62 if (!(value
& BIT(6))) {
69 value
= i2c_smbus_read_byte_data(client
, TC74_REG_TEMP
);
71 dev_dbg(&client
->dev
, "TC74_REG_TEMP read err %d\n",
78 data
->temp_input
= value
;
79 data
->next_update
= jiffies
+ HZ
/ 4;
84 mutex_unlock(&data
->lock
);
89 static ssize_t
show_temp_input(struct device
*dev
,
90 struct device_attribute
*attr
, char *buf
)
92 struct tc74_data
*data
= dev_get_drvdata(dev
);
95 ret
= tc74_update_device(dev
);
99 return sprintf(buf
, "%d\n", data
->temp_input
* 1000);
101 static SENSOR_DEVICE_ATTR(temp1_input
, S_IRUGO
, show_temp_input
, NULL
, 0);
103 static struct attribute
*tc74_attrs
[] = {
104 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
108 ATTRIBUTE_GROUPS(tc74
);
110 static int tc74_probe(struct i2c_client
*client
,
111 const struct i2c_device_id
*dev_id
)
113 struct device
*dev
= &client
->dev
;
114 struct tc74_data
*data
;
115 struct device
*hwmon_dev
;
118 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
121 data
= devm_kzalloc(dev
, sizeof(struct tc74_data
), GFP_KERNEL
);
125 data
->client
= client
;
126 mutex_init(&data
->lock
);
128 /* Make sure the chip is powered up. */
129 conf
= i2c_smbus_read_byte_data(client
, TC74_REG_CONFIG
);
131 dev_err(dev
, "unable to read config register\n");
137 dev_err(dev
, "invalid config register value\n");
147 ret
= i2c_smbus_write_byte_data(client
, TC74_REG_CONFIG
, conf
);
149 dev_warn(dev
, "unable to disable STANDBY\n");
152 hwmon_dev
= devm_hwmon_device_register_with_groups(dev
,
155 return PTR_ERR_OR_ZERO(hwmon_dev
);
158 static const struct i2c_device_id tc74_id
[] = {
162 MODULE_DEVICE_TABLE(i2c
, tc74_id
);
164 static struct i2c_driver tc74_driver
= {
172 module_i2c_driver(tc74_driver
);
174 MODULE_AUTHOR("Maciej Szmigiero <mail@maciej.szmigiero.name>");
176 MODULE_DESCRIPTION("TC74 driver");
177 MODULE_LICENSE("GPL");