xref: /openbmc/linux/drivers/hwmon/thmc50.c (revision b55f375725ff85aada394da488802b0a3cc99e88)
1add77c64SKrzysztof Helt /*
24d387df7SGuenter Roeck  * thmc50.c - Part of lm_sensors, Linux kernel modules for hardware
34d387df7SGuenter Roeck  *	      monitoring
44d387df7SGuenter Roeck  * Copyright (C) 2007 Krzysztof Helt <krzysztof.h1@wp.pl>
54d387df7SGuenter Roeck  * Based on 2.4 driver by Frodo Looijaard <frodol@dds.nl> and
64d387df7SGuenter Roeck  * Philip Edelbrock <phil@netroedge.com>
74d387df7SGuenter Roeck  *
84d387df7SGuenter Roeck  * This program is free software; you can redistribute it and/or modify
94d387df7SGuenter Roeck  * it under the terms of the GNU General Public License as published by
104d387df7SGuenter Roeck  * the Free Software Foundation; either version 2 of the License, or
114d387df7SGuenter Roeck  * (at your option) any later version.
124d387df7SGuenter Roeck  *
134d387df7SGuenter Roeck  * This program is distributed in the hope that it will be useful,
144d387df7SGuenter Roeck  * but WITHOUT ANY WARRANTY; without even the implied warranty of
154d387df7SGuenter Roeck  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
164d387df7SGuenter Roeck  * GNU General Public License for more details.
174d387df7SGuenter Roeck  *
184d387df7SGuenter Roeck  * You should have received a copy of the GNU General Public License
194d387df7SGuenter Roeck  * along with this program; if not, write to the Free Software
204d387df7SGuenter Roeck  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21add77c64SKrzysztof Helt  */
22add77c64SKrzysztof Helt 
23add77c64SKrzysztof Helt #include <linux/module.h>
24add77c64SKrzysztof Helt #include <linux/init.h>
25add77c64SKrzysztof Helt #include <linux/slab.h>
26add77c64SKrzysztof Helt #include <linux/i2c.h>
27add77c64SKrzysztof Helt #include <linux/hwmon.h>
28add77c64SKrzysztof Helt #include <linux/hwmon-sysfs.h>
29add77c64SKrzysztof Helt #include <linux/err.h>
30add77c64SKrzysztof Helt #include <linux/mutex.h>
31dcd8f392SJean Delvare #include <linux/jiffies.h>
32add77c64SKrzysztof Helt 
33add77c64SKrzysztof Helt MODULE_LICENSE("GPL");
34add77c64SKrzysztof Helt 
35add77c64SKrzysztof Helt /* Addresses to scan */
3625e9c86dSMark M. Hoffman static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
37add77c64SKrzysztof Helt 
38add77c64SKrzysztof Helt /* Insmod parameters */
39e5e9f44cSJean Delvare enum chips { thmc50, adm1022 };
40f95f0b4cSJean Delvare 
41f95f0b4cSJean Delvare static unsigned short adm1022_temp3[16];
42f95f0b4cSJean Delvare static unsigned int adm1022_temp3_num;
43f95f0b4cSJean Delvare module_param_array(adm1022_temp3, ushort, &adm1022_temp3_num, 0);
44*b55f3757SGuenter Roeck MODULE_PARM_DESC(adm1022_temp3,
45*b55f3757SGuenter Roeck 		 "List of adapter,address pairs to enable 3rd temperature (ADM1022 only)");
46add77c64SKrzysztof Helt 
47add77c64SKrzysztof Helt /* Many THMC50 constants specified below */
48add77c64SKrzysztof Helt 
49add77c64SKrzysztof Helt /* The THMC50 registers */
50add77c64SKrzysztof Helt #define THMC50_REG_CONF				0x40
51add77c64SKrzysztof Helt #define THMC50_REG_COMPANY_ID			0x3E
52add77c64SKrzysztof Helt #define THMC50_REG_DIE_CODE			0x3F
53add77c64SKrzysztof Helt #define THMC50_REG_ANALOG_OUT			0x19
54dcf3b5fbSKrzysztof Helt /*
55bba891c2SKrzysztof Helt  * The mirror status register cannot be used as
56bba891c2SKrzysztof Helt  * reading it does not clear alarms.
57dcf3b5fbSKrzysztof Helt  */
58bba891c2SKrzysztof Helt #define THMC50_REG_INTR				0x41
59add77c64SKrzysztof Helt 
605910a9b2STobias Klauser static const u8 THMC50_REG_TEMP[] = { 0x27, 0x26, 0x20 };
615910a9b2STobias Klauser static const u8 THMC50_REG_TEMP_MIN[] = { 0x3A, 0x38, 0x2C };
625910a9b2STobias Klauser static const u8 THMC50_REG_TEMP_MAX[] = { 0x39, 0x37, 0x2B };
6384f768c1SKrzysztof Helt static const u8 THMC50_REG_TEMP_CRITICAL[] = { 0x13, 0x14, 0x14 };
6484f768c1SKrzysztof Helt static const u8 THMC50_REG_TEMP_DEFAULT[] = { 0x17, 0x18, 0x18 };
65add77c64SKrzysztof Helt 
66add77c64SKrzysztof Helt #define THMC50_REG_CONF_nFANOFF			0x20
6784f768c1SKrzysztof Helt #define THMC50_REG_CONF_PROGRAMMED		0x08
68add77c64SKrzysztof Helt 
69add77c64SKrzysztof Helt /* Each client has this additional data */
70add77c64SKrzysztof Helt struct thmc50_data {
711beeffe4STony Jones 	struct device *hwmon_dev;
72add77c64SKrzysztof Helt 
73add77c64SKrzysztof Helt 	struct mutex update_lock;
74add77c64SKrzysztof Helt 	enum chips type;
75add77c64SKrzysztof Helt 	unsigned long last_updated;	/* In jiffies */
76add77c64SKrzysztof Helt 	char has_temp3;		/* !=0 if it is ADM1022 in temp3 mode */
77add77c64SKrzysztof Helt 	char valid;		/* !=0 if following fields are valid */
78add77c64SKrzysztof Helt 
79add77c64SKrzysztof Helt 	/* Register values */
80add77c64SKrzysztof Helt 	s8 temp_input[3];
81add77c64SKrzysztof Helt 	s8 temp_max[3];
82add77c64SKrzysztof Helt 	s8 temp_min[3];
8384f768c1SKrzysztof Helt 	s8 temp_critical[3];
84add77c64SKrzysztof Helt 	u8 analog_out;
85dcf3b5fbSKrzysztof Helt 	u8 alarms;
86add77c64SKrzysztof Helt };
87add77c64SKrzysztof Helt 
88310ec792SJean Delvare static int thmc50_detect(struct i2c_client *client,
89ccf37488SJean Delvare 			 struct i2c_board_info *info);
90ccf37488SJean Delvare static int thmc50_probe(struct i2c_client *client,
91ccf37488SJean Delvare 			const struct i2c_device_id *id);
92ccf37488SJean Delvare static int thmc50_remove(struct i2c_client *client);
93add77c64SKrzysztof Helt static void thmc50_init_client(struct i2c_client *client);
94add77c64SKrzysztof Helt static struct thmc50_data *thmc50_update_device(struct device *dev);
95add77c64SKrzysztof Helt 
96ccf37488SJean Delvare static const struct i2c_device_id thmc50_id[] = {
97ccf37488SJean Delvare 	{ "adm1022", adm1022 },
98ccf37488SJean Delvare 	{ "thmc50", thmc50 },
99ccf37488SJean Delvare 	{ }
100ccf37488SJean Delvare };
101ccf37488SJean Delvare MODULE_DEVICE_TABLE(i2c, thmc50_id);
102ccf37488SJean Delvare 
103add77c64SKrzysztof Helt static struct i2c_driver thmc50_driver = {
104ccf37488SJean Delvare 	.class = I2C_CLASS_HWMON,
105add77c64SKrzysztof Helt 	.driver = {
106add77c64SKrzysztof Helt 		.name = "thmc50",
107add77c64SKrzysztof Helt 	},
108ccf37488SJean Delvare 	.probe = thmc50_probe,
109ccf37488SJean Delvare 	.remove = thmc50_remove,
110ccf37488SJean Delvare 	.id_table = thmc50_id,
111ccf37488SJean Delvare 	.detect = thmc50_detect,
112c3813d6aSJean Delvare 	.address_list = normal_i2c,
113add77c64SKrzysztof Helt };
114add77c64SKrzysztof Helt 
115add77c64SKrzysztof Helt static ssize_t show_analog_out(struct device *dev,
116add77c64SKrzysztof Helt 			       struct device_attribute *attr, char *buf)
117add77c64SKrzysztof Helt {
118add77c64SKrzysztof Helt 	struct thmc50_data *data = thmc50_update_device(dev);
119add77c64SKrzysztof Helt 	return sprintf(buf, "%d\n", data->analog_out);
120add77c64SKrzysztof Helt }
121add77c64SKrzysztof Helt 
122add77c64SKrzysztof Helt static ssize_t set_analog_out(struct device *dev,
123add77c64SKrzysztof Helt 			      struct device_attribute *attr,
124add77c64SKrzysztof Helt 			      const char *buf, size_t count)
125add77c64SKrzysztof Helt {
126add77c64SKrzysztof Helt 	struct i2c_client *client = to_i2c_client(dev);
127add77c64SKrzysztof Helt 	struct thmc50_data *data = i2c_get_clientdata(client);
128add77c64SKrzysztof Helt 	int config;
1294d387df7SGuenter Roeck 	unsigned long tmp;
1304d387df7SGuenter Roeck 	int err;
1314d387df7SGuenter Roeck 
1324d387df7SGuenter Roeck 	err = kstrtoul(buf, 10, &tmp);
1334d387df7SGuenter Roeck 	if (err)
1344d387df7SGuenter Roeck 		return err;
135add77c64SKrzysztof Helt 
136add77c64SKrzysztof Helt 	mutex_lock(&data->update_lock);
1372a844c14SGuenter Roeck 	data->analog_out = clamp_val(tmp, 0, 255);
138add77c64SKrzysztof Helt 	i2c_smbus_write_byte_data(client, THMC50_REG_ANALOG_OUT,
139add77c64SKrzysztof Helt 				  data->analog_out);
140add77c64SKrzysztof Helt 
141add77c64SKrzysztof Helt 	config = i2c_smbus_read_byte_data(client, THMC50_REG_CONF);
142add77c64SKrzysztof Helt 	if (data->analog_out == 0)
143add77c64SKrzysztof Helt 		config &= ~THMC50_REG_CONF_nFANOFF;
144add77c64SKrzysztof Helt 	else
145add77c64SKrzysztof Helt 		config |= THMC50_REG_CONF_nFANOFF;
146add77c64SKrzysztof Helt 	i2c_smbus_write_byte_data(client, THMC50_REG_CONF, config);
147add77c64SKrzysztof Helt 
148add77c64SKrzysztof Helt 	mutex_unlock(&data->update_lock);
149add77c64SKrzysztof Helt 	return count;
150add77c64SKrzysztof Helt }
151add77c64SKrzysztof Helt 
152add77c64SKrzysztof Helt /* There is only one PWM mode = DC */
153add77c64SKrzysztof Helt static ssize_t show_pwm_mode(struct device *dev, struct device_attribute *attr,
154add77c64SKrzysztof Helt 			     char *buf)
155add77c64SKrzysztof Helt {
156add77c64SKrzysztof Helt 	return sprintf(buf, "0\n");
157add77c64SKrzysztof Helt }
158add77c64SKrzysztof Helt 
159add77c64SKrzysztof Helt /* Temperatures */
160add77c64SKrzysztof Helt static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
161add77c64SKrzysztof Helt 			 char *buf)
162add77c64SKrzysztof Helt {
163add77c64SKrzysztof Helt 	int nr = to_sensor_dev_attr(attr)->index;
164add77c64SKrzysztof Helt 	struct thmc50_data *data = thmc50_update_device(dev);
165add77c64SKrzysztof Helt 	return sprintf(buf, "%d\n", data->temp_input[nr] * 1000);
166add77c64SKrzysztof Helt }
167add77c64SKrzysztof Helt 
168add77c64SKrzysztof Helt static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
169add77c64SKrzysztof Helt 			     char *buf)
170add77c64SKrzysztof Helt {
171add77c64SKrzysztof Helt 	int nr = to_sensor_dev_attr(attr)->index;
172add77c64SKrzysztof Helt 	struct thmc50_data *data = thmc50_update_device(dev);
173add77c64SKrzysztof Helt 	return sprintf(buf, "%d\n", data->temp_min[nr] * 1000);
174add77c64SKrzysztof Helt }
175add77c64SKrzysztof Helt 
176add77c64SKrzysztof Helt static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
177add77c64SKrzysztof Helt 			    const char *buf, size_t count)
178add77c64SKrzysztof Helt {
179add77c64SKrzysztof Helt 	int nr = to_sensor_dev_attr(attr)->index;
180add77c64SKrzysztof Helt 	struct i2c_client *client = to_i2c_client(dev);
181add77c64SKrzysztof Helt 	struct thmc50_data *data = i2c_get_clientdata(client);
1824d387df7SGuenter Roeck 	long val;
1834d387df7SGuenter Roeck 	int err;
1844d387df7SGuenter Roeck 
1854d387df7SGuenter Roeck 	err = kstrtol(buf, 10, &val);
1864d387df7SGuenter Roeck 	if (err)
1874d387df7SGuenter Roeck 		return err;
188add77c64SKrzysztof Helt 
189add77c64SKrzysztof Helt 	mutex_lock(&data->update_lock);
1902a844c14SGuenter Roeck 	data->temp_min[nr] = clamp_val(val / 1000, -128, 127);
191add77c64SKrzysztof Helt 	i2c_smbus_write_byte_data(client, THMC50_REG_TEMP_MIN[nr],
192add77c64SKrzysztof Helt 				  data->temp_min[nr]);
193add77c64SKrzysztof Helt 	mutex_unlock(&data->update_lock);
194add77c64SKrzysztof Helt 	return count;
195add77c64SKrzysztof Helt }
196add77c64SKrzysztof Helt 
197add77c64SKrzysztof Helt static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
198add77c64SKrzysztof Helt 			     char *buf)
199add77c64SKrzysztof Helt {
200add77c64SKrzysztof Helt 	int nr = to_sensor_dev_attr(attr)->index;
201add77c64SKrzysztof Helt 	struct thmc50_data *data = thmc50_update_device(dev);
202add77c64SKrzysztof Helt 	return sprintf(buf, "%d\n", data->temp_max[nr] * 1000);
203add77c64SKrzysztof Helt }
204add77c64SKrzysztof Helt 
205add77c64SKrzysztof Helt static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
206add77c64SKrzysztof Helt 			    const char *buf, size_t count)
207add77c64SKrzysztof Helt {
208add77c64SKrzysztof Helt 	int nr = to_sensor_dev_attr(attr)->index;
209add77c64SKrzysztof Helt 	struct i2c_client *client = to_i2c_client(dev);
210add77c64SKrzysztof Helt 	struct thmc50_data *data = i2c_get_clientdata(client);
2114d387df7SGuenter Roeck 	long val;
2124d387df7SGuenter Roeck 	int err;
2134d387df7SGuenter Roeck 
2144d387df7SGuenter Roeck 	err = kstrtol(buf, 10, &val);
2154d387df7SGuenter Roeck 	if (err)
2164d387df7SGuenter Roeck 		return err;
217add77c64SKrzysztof Helt 
218add77c64SKrzysztof Helt 	mutex_lock(&data->update_lock);
2192a844c14SGuenter Roeck 	data->temp_max[nr] = clamp_val(val / 1000, -128, 127);
220add77c64SKrzysztof Helt 	i2c_smbus_write_byte_data(client, THMC50_REG_TEMP_MAX[nr],
221add77c64SKrzysztof Helt 				  data->temp_max[nr]);
222add77c64SKrzysztof Helt 	mutex_unlock(&data->update_lock);
223add77c64SKrzysztof Helt 	return count;
224add77c64SKrzysztof Helt }
225add77c64SKrzysztof Helt 
22684f768c1SKrzysztof Helt static ssize_t show_temp_critical(struct device *dev,
22784f768c1SKrzysztof Helt 				  struct device_attribute *attr,
22884f768c1SKrzysztof Helt 				  char *buf)
22984f768c1SKrzysztof Helt {
23084f768c1SKrzysztof Helt 	int nr = to_sensor_dev_attr(attr)->index;
23184f768c1SKrzysztof Helt 	struct thmc50_data *data = thmc50_update_device(dev);
23284f768c1SKrzysztof Helt 	return sprintf(buf, "%d\n", data->temp_critical[nr] * 1000);
23384f768c1SKrzysztof Helt }
23484f768c1SKrzysztof Helt 
235dcf3b5fbSKrzysztof Helt static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
236dcf3b5fbSKrzysztof Helt 			  char *buf)
237dcf3b5fbSKrzysztof Helt {
238dcf3b5fbSKrzysztof Helt 	int index = to_sensor_dev_attr(attr)->index;
239dcf3b5fbSKrzysztof Helt 	struct thmc50_data *data = thmc50_update_device(dev);
240dcf3b5fbSKrzysztof Helt 
241dcf3b5fbSKrzysztof Helt 	return sprintf(buf, "%u\n", (data->alarms >> index) & 1);
242dcf3b5fbSKrzysztof Helt }
243dcf3b5fbSKrzysztof Helt 
244add77c64SKrzysztof Helt #define temp_reg(offset)						\
245add77c64SKrzysztof Helt static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp,	\
246add77c64SKrzysztof Helt 			NULL, offset - 1);				\
247add77c64SKrzysztof Helt static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR,	\
248add77c64SKrzysztof Helt 			show_temp_min, set_temp_min, offset - 1);	\
249add77c64SKrzysztof Helt static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR,	\
25084f768c1SKrzysztof Helt 			show_temp_max, set_temp_max, offset - 1);	\
25184f768c1SKrzysztof Helt static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IRUGO,			\
25284f768c1SKrzysztof Helt 			show_temp_critical, NULL, offset - 1);
253add77c64SKrzysztof Helt 
254add77c64SKrzysztof Helt temp_reg(1);
255add77c64SKrzysztof Helt temp_reg(2);
256add77c64SKrzysztof Helt temp_reg(3);
257add77c64SKrzysztof Helt 
258dcf3b5fbSKrzysztof Helt static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 0);
259dcf3b5fbSKrzysztof Helt static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
260dcf3b5fbSKrzysztof Helt static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 1);
261dcf3b5fbSKrzysztof Helt static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 7);
262dcf3b5fbSKrzysztof Helt static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 2);
263dcf3b5fbSKrzysztof Helt 
264add77c64SKrzysztof Helt static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_analog_out,
265add77c64SKrzysztof Helt 			  set_analog_out, 0);
266add77c64SKrzysztof Helt static SENSOR_DEVICE_ATTR(pwm1_mode, S_IRUGO, show_pwm_mode, NULL, 0);
267add77c64SKrzysztof Helt 
268add77c64SKrzysztof Helt static struct attribute *thmc50_attributes[] = {
269add77c64SKrzysztof Helt 	&sensor_dev_attr_temp1_max.dev_attr.attr,
270add77c64SKrzysztof Helt 	&sensor_dev_attr_temp1_min.dev_attr.attr,
271add77c64SKrzysztof Helt 	&sensor_dev_attr_temp1_input.dev_attr.attr,
27284f768c1SKrzysztof Helt 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
273dcf3b5fbSKrzysztof Helt 	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
274add77c64SKrzysztof Helt 	&sensor_dev_attr_temp2_max.dev_attr.attr,
275add77c64SKrzysztof Helt 	&sensor_dev_attr_temp2_min.dev_attr.attr,
276add77c64SKrzysztof Helt 	&sensor_dev_attr_temp2_input.dev_attr.attr,
27784f768c1SKrzysztof Helt 	&sensor_dev_attr_temp2_crit.dev_attr.attr,
278dcf3b5fbSKrzysztof Helt 	&sensor_dev_attr_temp2_alarm.dev_attr.attr,
279dcf3b5fbSKrzysztof Helt 	&sensor_dev_attr_temp2_fault.dev_attr.attr,
280add77c64SKrzysztof Helt 	&sensor_dev_attr_pwm1.dev_attr.attr,
281add77c64SKrzysztof Helt 	&sensor_dev_attr_pwm1_mode.dev_attr.attr,
282add77c64SKrzysztof Helt 	NULL
283add77c64SKrzysztof Helt };
284add77c64SKrzysztof Helt 
285add77c64SKrzysztof Helt static const struct attribute_group thmc50_group = {
286add77c64SKrzysztof Helt 	.attrs = thmc50_attributes,
287add77c64SKrzysztof Helt };
288add77c64SKrzysztof Helt 
289add77c64SKrzysztof Helt /* for ADM1022 3rd temperature mode */
290894c00cfSJean Delvare static struct attribute *temp3_attributes[] = {
291add77c64SKrzysztof Helt 	&sensor_dev_attr_temp3_max.dev_attr.attr,
292add77c64SKrzysztof Helt 	&sensor_dev_attr_temp3_min.dev_attr.attr,
293add77c64SKrzysztof Helt 	&sensor_dev_attr_temp3_input.dev_attr.attr,
29484f768c1SKrzysztof Helt 	&sensor_dev_attr_temp3_crit.dev_attr.attr,
295dcf3b5fbSKrzysztof Helt 	&sensor_dev_attr_temp3_alarm.dev_attr.attr,
296dcf3b5fbSKrzysztof Helt 	&sensor_dev_attr_temp3_fault.dev_attr.attr,
297add77c64SKrzysztof Helt 	NULL
298add77c64SKrzysztof Helt };
299add77c64SKrzysztof Helt 
300894c00cfSJean Delvare static const struct attribute_group temp3_group = {
301894c00cfSJean Delvare 	.attrs = temp3_attributes,
302add77c64SKrzysztof Helt };
303add77c64SKrzysztof Helt 
304ccf37488SJean Delvare /* Return 0 if detection is successful, -ENODEV otherwise */
305310ec792SJean Delvare static int thmc50_detect(struct i2c_client *client,
306ccf37488SJean Delvare 			 struct i2c_board_info *info)
307add77c64SKrzysztof Helt {
308add77c64SKrzysztof Helt 	unsigned company;
309add77c64SKrzysztof Helt 	unsigned revision;
310add77c64SKrzysztof Helt 	unsigned config;
311ccf37488SJean Delvare 	struct i2c_adapter *adapter = client->adapter;
312cc28a610SJean Delvare 	const char *type_name;
313add77c64SKrzysztof Helt 
314add77c64SKrzysztof Helt 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
315*b55f3757SGuenter Roeck 		pr_debug("thmc50: detect failed, smbus byte data not supported!\n");
316ccf37488SJean Delvare 		return -ENODEV;
317add77c64SKrzysztof Helt 	}
318add77c64SKrzysztof Helt 
319add77c64SKrzysztof Helt 	pr_debug("thmc50: Probing for THMC50 at 0x%2X on bus %d\n",
320add77c64SKrzysztof Helt 		 client->addr, i2c_adapter_id(client->adapter));
321add77c64SKrzysztof Helt 
322add77c64SKrzysztof Helt 	company = i2c_smbus_read_byte_data(client, THMC50_REG_COMPANY_ID);
323add77c64SKrzysztof Helt 	revision = i2c_smbus_read_byte_data(client, THMC50_REG_DIE_CODE);
324add77c64SKrzysztof Helt 	config = i2c_smbus_read_byte_data(client, THMC50_REG_CONF);
32552df6440SJean Delvare 	if (revision < 0xc0 || (config & 0x10))
32652df6440SJean Delvare 		return -ENODEV;
327add77c64SKrzysztof Helt 
32852df6440SJean Delvare 	if (company == 0x41) {
329add77c64SKrzysztof Helt 		int id = i2c_adapter_id(client->adapter);
330add77c64SKrzysztof Helt 		int i;
331add77c64SKrzysztof Helt 
332add77c64SKrzysztof Helt 		type_name = "adm1022";
333add77c64SKrzysztof Helt 		for (i = 0; i + 1 < adm1022_temp3_num; i += 2)
334add77c64SKrzysztof Helt 			if (adm1022_temp3[i] == id &&
335ccf37488SJean Delvare 			    adm1022_temp3[i + 1] == client->addr) {
336add77c64SKrzysztof Helt 				/* enable 2nd remote temp */
337ccf37488SJean Delvare 				config |= (1 << 7);
338ccf37488SJean Delvare 				i2c_smbus_write_byte_data(client,
339ccf37488SJean Delvare 							  THMC50_REG_CONF,
340ccf37488SJean Delvare 							  config);
341add77c64SKrzysztof Helt 				break;
342add77c64SKrzysztof Helt 			}
34352df6440SJean Delvare 	} else if (company == 0x49) {
344cc28a610SJean Delvare 		type_name = "thmc50";
34552df6440SJean Delvare 	} else {
34652df6440SJean Delvare 		pr_debug("thmc50: Detection of THMC50/ADM1022 failed\n");
34752df6440SJean Delvare 		return -ENODEV;
348add77c64SKrzysztof Helt 	}
34952df6440SJean Delvare 
350cc28a610SJean Delvare 	pr_debug("thmc50: Detected %s (version %x, revision %x)\n",
351cc28a610SJean Delvare 		 type_name, (revision >> 4) - 0xc, revision & 0xf);
352add77c64SKrzysztof Helt 
353ccf37488SJean Delvare 	strlcpy(info->type, type_name, I2C_NAME_SIZE);
354add77c64SKrzysztof Helt 
355ccf37488SJean Delvare 	return 0;
356ccf37488SJean Delvare }
357ccf37488SJean Delvare 
358ccf37488SJean Delvare static int thmc50_probe(struct i2c_client *client,
359ccf37488SJean Delvare 			const struct i2c_device_id *id)
360ccf37488SJean Delvare {
361ccf37488SJean Delvare 	struct thmc50_data *data;
362ccf37488SJean Delvare 	int err;
363ccf37488SJean Delvare 
364bd91d3baSGuenter Roeck 	data = devm_kzalloc(&client->dev, sizeof(struct thmc50_data),
365bd91d3baSGuenter Roeck 			    GFP_KERNEL);
366bd91d3baSGuenter Roeck 	if (!data)
367bd91d3baSGuenter Roeck 		return -ENOMEM;
368ccf37488SJean Delvare 
369ccf37488SJean Delvare 	i2c_set_clientdata(client, data);
370ccf37488SJean Delvare 	data->type = id->driver_data;
371ccf37488SJean Delvare 	mutex_init(&data->update_lock);
372add77c64SKrzysztof Helt 
373add77c64SKrzysztof Helt 	thmc50_init_client(client);
374add77c64SKrzysztof Helt 
375add77c64SKrzysztof Helt 	/* Register sysfs hooks */
3764d387df7SGuenter Roeck 	err = sysfs_create_group(&client->dev.kobj, &thmc50_group);
3774d387df7SGuenter Roeck 	if (err)
378bd91d3baSGuenter Roeck 		return err;
379add77c64SKrzysztof Helt 
380add77c64SKrzysztof Helt 	/* Register ADM1022 sysfs hooks */
3814d387df7SGuenter Roeck 	if (data->has_temp3) {
3824d387df7SGuenter Roeck 		err = sysfs_create_group(&client->dev.kobj, &temp3_group);
3834d387df7SGuenter Roeck 		if (err)
384add77c64SKrzysztof Helt 			goto exit_remove_sysfs_thmc50;
3854d387df7SGuenter Roeck 	}
386add77c64SKrzysztof Helt 
387add77c64SKrzysztof Helt 	/* Register a new directory entry with module sensors */
3881beeffe4STony Jones 	data->hwmon_dev = hwmon_device_register(&client->dev);
3891beeffe4STony Jones 	if (IS_ERR(data->hwmon_dev)) {
3901beeffe4STony Jones 		err = PTR_ERR(data->hwmon_dev);
391add77c64SKrzysztof Helt 		goto exit_remove_sysfs;
392add77c64SKrzysztof Helt 	}
393add77c64SKrzysztof Helt 
394add77c64SKrzysztof Helt 	return 0;
395add77c64SKrzysztof Helt 
396add77c64SKrzysztof Helt exit_remove_sysfs:
397894c00cfSJean Delvare 	if (data->has_temp3)
398894c00cfSJean Delvare 		sysfs_remove_group(&client->dev.kobj, &temp3_group);
399add77c64SKrzysztof Helt exit_remove_sysfs_thmc50:
400add77c64SKrzysztof Helt 	sysfs_remove_group(&client->dev.kobj, &thmc50_group);
401add77c64SKrzysztof Helt 	return err;
402add77c64SKrzysztof Helt }
403add77c64SKrzysztof Helt 
404ccf37488SJean Delvare static int thmc50_remove(struct i2c_client *client)
405add77c64SKrzysztof Helt {
406add77c64SKrzysztof Helt 	struct thmc50_data *data = i2c_get_clientdata(client);
407add77c64SKrzysztof Helt 
4081beeffe4STony Jones 	hwmon_device_unregister(data->hwmon_dev);
409add77c64SKrzysztof Helt 	sysfs_remove_group(&client->dev.kobj, &thmc50_group);
410894c00cfSJean Delvare 	if (data->has_temp3)
411894c00cfSJean Delvare 		sysfs_remove_group(&client->dev.kobj, &temp3_group);
412add77c64SKrzysztof Helt 
413add77c64SKrzysztof Helt 	return 0;
414add77c64SKrzysztof Helt }
415add77c64SKrzysztof Helt 
416add77c64SKrzysztof Helt static void thmc50_init_client(struct i2c_client *client)
417add77c64SKrzysztof Helt {
418add77c64SKrzysztof Helt 	struct thmc50_data *data = i2c_get_clientdata(client);
419add77c64SKrzysztof Helt 	int config;
420add77c64SKrzysztof Helt 
421add77c64SKrzysztof Helt 	data->analog_out = i2c_smbus_read_byte_data(client,
422add77c64SKrzysztof Helt 						    THMC50_REG_ANALOG_OUT);
423add77c64SKrzysztof Helt 	/* set up to at least 1 */
424add77c64SKrzysztof Helt 	if (data->analog_out == 0) {
425add77c64SKrzysztof Helt 		data->analog_out = 1;
426add77c64SKrzysztof Helt 		i2c_smbus_write_byte_data(client, THMC50_REG_ANALOG_OUT,
427add77c64SKrzysztof Helt 					  data->analog_out);
428add77c64SKrzysztof Helt 	}
429add77c64SKrzysztof Helt 	config = i2c_smbus_read_byte_data(client, THMC50_REG_CONF);
430add77c64SKrzysztof Helt 	config |= 0x1;	/* start the chip if it is in standby mode */
431ccf37488SJean Delvare 	if (data->type == adm1022 && (config & (1 << 7)))
432ccf37488SJean Delvare 		data->has_temp3 = 1;
433add77c64SKrzysztof Helt 	i2c_smbus_write_byte_data(client, THMC50_REG_CONF, config);
434add77c64SKrzysztof Helt }
435add77c64SKrzysztof Helt 
436add77c64SKrzysztof Helt static struct thmc50_data *thmc50_update_device(struct device *dev)
437add77c64SKrzysztof Helt {
438add77c64SKrzysztof Helt 	struct i2c_client *client = to_i2c_client(dev);
439add77c64SKrzysztof Helt 	struct thmc50_data *data = i2c_get_clientdata(client);
440add77c64SKrzysztof Helt 	int timeout = HZ / 5 + (data->type == thmc50 ? HZ : 0);
441add77c64SKrzysztof Helt 
442add77c64SKrzysztof Helt 	mutex_lock(&data->update_lock);
443add77c64SKrzysztof Helt 
444add77c64SKrzysztof Helt 	if (time_after(jiffies, data->last_updated + timeout)
445add77c64SKrzysztof Helt 	    || !data->valid) {
446add77c64SKrzysztof Helt 
447add77c64SKrzysztof Helt 		int temps = data->has_temp3 ? 3 : 2;
448add77c64SKrzysztof Helt 		int i;
44984f768c1SKrzysztof Helt 		int prog = i2c_smbus_read_byte_data(client, THMC50_REG_CONF);
45084f768c1SKrzysztof Helt 
45184f768c1SKrzysztof Helt 		prog &= THMC50_REG_CONF_PROGRAMMED;
45284f768c1SKrzysztof Helt 
453add77c64SKrzysztof Helt 		for (i = 0; i < temps; i++) {
454add77c64SKrzysztof Helt 			data->temp_input[i] = i2c_smbus_read_byte_data(client,
455add77c64SKrzysztof Helt 						THMC50_REG_TEMP[i]);
456add77c64SKrzysztof Helt 			data->temp_max[i] = i2c_smbus_read_byte_data(client,
457add77c64SKrzysztof Helt 						THMC50_REG_TEMP_MAX[i]);
458add77c64SKrzysztof Helt 			data->temp_min[i] = i2c_smbus_read_byte_data(client,
459add77c64SKrzysztof Helt 						THMC50_REG_TEMP_MIN[i]);
46084f768c1SKrzysztof Helt 			data->temp_critical[i] =
46184f768c1SKrzysztof Helt 				i2c_smbus_read_byte_data(client,
46284f768c1SKrzysztof Helt 					prog ? THMC50_REG_TEMP_CRITICAL[i]
46384f768c1SKrzysztof Helt 					     : THMC50_REG_TEMP_DEFAULT[i]);
464add77c64SKrzysztof Helt 		}
465add77c64SKrzysztof Helt 		data->analog_out =
466add77c64SKrzysztof Helt 		    i2c_smbus_read_byte_data(client, THMC50_REG_ANALOG_OUT);
467dcf3b5fbSKrzysztof Helt 		data->alarms =
468bba891c2SKrzysztof Helt 		    i2c_smbus_read_byte_data(client, THMC50_REG_INTR);
469add77c64SKrzysztof Helt 		data->last_updated = jiffies;
470add77c64SKrzysztof Helt 		data->valid = 1;
471add77c64SKrzysztof Helt 	}
472add77c64SKrzysztof Helt 
473add77c64SKrzysztof Helt 	mutex_unlock(&data->update_lock);
474add77c64SKrzysztof Helt 
475add77c64SKrzysztof Helt 	return data;
476add77c64SKrzysztof Helt }
477add77c64SKrzysztof Helt 
478f0967eeaSAxel Lin module_i2c_driver(thmc50_driver);
479add77c64SKrzysztof Helt 
480add77c64SKrzysztof Helt MODULE_AUTHOR("Krzysztof Helt <krzysztof.h1@wp.pl>");
481add77c64SKrzysztof Helt MODULE_DESCRIPTION("THMC50 driver");
482