xref: /openbmc/linux/drivers/hwmon/thmc50.c (revision bd91d3bab8787f53c1b937ac817b0c59f8146b97)
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>
31add77c64SKrzysztof Helt 
32add77c64SKrzysztof Helt MODULE_LICENSE("GPL");
33add77c64SKrzysztof Helt 
34add77c64SKrzysztof Helt /* Addresses to scan */
3525e9c86dSMark M. Hoffman static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
36add77c64SKrzysztof Helt 
37add77c64SKrzysztof Helt /* Insmod parameters */
38e5e9f44cSJean Delvare enum chips { thmc50, adm1022 };
39f95f0b4cSJean Delvare 
40f95f0b4cSJean Delvare static unsigned short adm1022_temp3[16];
41f95f0b4cSJean Delvare static unsigned int adm1022_temp3_num;
42f95f0b4cSJean Delvare module_param_array(adm1022_temp3, ushort, &adm1022_temp3_num, 0);
43f95f0b4cSJean Delvare MODULE_PARM_DESC(adm1022_temp3, "List of adapter,address pairs "
44add77c64SKrzysztof Helt 			"to enable 3rd temperature (ADM1022 only)");
45add77c64SKrzysztof Helt 
46add77c64SKrzysztof Helt /* Many THMC50 constants specified below */
47add77c64SKrzysztof Helt 
48add77c64SKrzysztof Helt /* The THMC50 registers */
49add77c64SKrzysztof Helt #define THMC50_REG_CONF				0x40
50add77c64SKrzysztof Helt #define THMC50_REG_COMPANY_ID			0x3E
51add77c64SKrzysztof Helt #define THMC50_REG_DIE_CODE			0x3F
52add77c64SKrzysztof Helt #define THMC50_REG_ANALOG_OUT			0x19
53dcf3b5fbSKrzysztof Helt /*
54bba891c2SKrzysztof Helt  * The mirror status register cannot be used as
55bba891c2SKrzysztof Helt  * reading it does not clear alarms.
56dcf3b5fbSKrzysztof Helt  */
57bba891c2SKrzysztof Helt #define THMC50_REG_INTR				0x41
58add77c64SKrzysztof Helt 
595910a9b2STobias Klauser static const u8 THMC50_REG_TEMP[] = { 0x27, 0x26, 0x20 };
605910a9b2STobias Klauser static const u8 THMC50_REG_TEMP_MIN[] = { 0x3A, 0x38, 0x2C };
615910a9b2STobias Klauser static const u8 THMC50_REG_TEMP_MAX[] = { 0x39, 0x37, 0x2B };
6284f768c1SKrzysztof Helt static const u8 THMC50_REG_TEMP_CRITICAL[] = { 0x13, 0x14, 0x14 };
6384f768c1SKrzysztof Helt static const u8 THMC50_REG_TEMP_DEFAULT[] = { 0x17, 0x18, 0x18 };
64add77c64SKrzysztof Helt 
65add77c64SKrzysztof Helt #define THMC50_REG_CONF_nFANOFF			0x20
6684f768c1SKrzysztof Helt #define THMC50_REG_CONF_PROGRAMMED		0x08
67add77c64SKrzysztof Helt 
68add77c64SKrzysztof Helt /* Each client has this additional data */
69add77c64SKrzysztof Helt struct thmc50_data {
701beeffe4STony Jones 	struct device *hwmon_dev;
71add77c64SKrzysztof Helt 
72add77c64SKrzysztof Helt 	struct mutex update_lock;
73add77c64SKrzysztof Helt 	enum chips type;
74add77c64SKrzysztof Helt 	unsigned long last_updated;	/* In jiffies */
75add77c64SKrzysztof Helt 	char has_temp3;		/* !=0 if it is ADM1022 in temp3 mode */
76add77c64SKrzysztof Helt 	char valid;		/* !=0 if following fields are valid */
77add77c64SKrzysztof Helt 
78add77c64SKrzysztof Helt 	/* Register values */
79add77c64SKrzysztof Helt 	s8 temp_input[3];
80add77c64SKrzysztof Helt 	s8 temp_max[3];
81add77c64SKrzysztof Helt 	s8 temp_min[3];
8284f768c1SKrzysztof Helt 	s8 temp_critical[3];
83add77c64SKrzysztof Helt 	u8 analog_out;
84dcf3b5fbSKrzysztof Helt 	u8 alarms;
85add77c64SKrzysztof Helt };
86add77c64SKrzysztof Helt 
87310ec792SJean Delvare static int thmc50_detect(struct i2c_client *client,
88ccf37488SJean Delvare 			 struct i2c_board_info *info);
89ccf37488SJean Delvare static int thmc50_probe(struct i2c_client *client,
90ccf37488SJean Delvare 			const struct i2c_device_id *id);
91ccf37488SJean Delvare static int thmc50_remove(struct i2c_client *client);
92add77c64SKrzysztof Helt static void thmc50_init_client(struct i2c_client *client);
93add77c64SKrzysztof Helt static struct thmc50_data *thmc50_update_device(struct device *dev);
94add77c64SKrzysztof Helt 
95ccf37488SJean Delvare static const struct i2c_device_id thmc50_id[] = {
96ccf37488SJean Delvare 	{ "adm1022", adm1022 },
97ccf37488SJean Delvare 	{ "thmc50", thmc50 },
98ccf37488SJean Delvare 	{ }
99ccf37488SJean Delvare };
100ccf37488SJean Delvare MODULE_DEVICE_TABLE(i2c, thmc50_id);
101ccf37488SJean Delvare 
102add77c64SKrzysztof Helt static struct i2c_driver thmc50_driver = {
103ccf37488SJean Delvare 	.class = I2C_CLASS_HWMON,
104add77c64SKrzysztof Helt 	.driver = {
105add77c64SKrzysztof Helt 		.name = "thmc50",
106add77c64SKrzysztof Helt 	},
107ccf37488SJean Delvare 	.probe = thmc50_probe,
108ccf37488SJean Delvare 	.remove = thmc50_remove,
109ccf37488SJean Delvare 	.id_table = thmc50_id,
110ccf37488SJean Delvare 	.detect = thmc50_detect,
111c3813d6aSJean Delvare 	.address_list = normal_i2c,
112add77c64SKrzysztof Helt };
113add77c64SKrzysztof Helt 
114add77c64SKrzysztof Helt static ssize_t show_analog_out(struct device *dev,
115add77c64SKrzysztof Helt 			       struct device_attribute *attr, char *buf)
116add77c64SKrzysztof Helt {
117add77c64SKrzysztof Helt 	struct thmc50_data *data = thmc50_update_device(dev);
118add77c64SKrzysztof Helt 	return sprintf(buf, "%d\n", data->analog_out);
119add77c64SKrzysztof Helt }
120add77c64SKrzysztof Helt 
121add77c64SKrzysztof Helt static ssize_t set_analog_out(struct device *dev,
122add77c64SKrzysztof Helt 			      struct device_attribute *attr,
123add77c64SKrzysztof Helt 			      const char *buf, size_t count)
124add77c64SKrzysztof Helt {
125add77c64SKrzysztof Helt 	struct i2c_client *client = to_i2c_client(dev);
126add77c64SKrzysztof Helt 	struct thmc50_data *data = i2c_get_clientdata(client);
127add77c64SKrzysztof Helt 	int config;
1284d387df7SGuenter Roeck 	unsigned long tmp;
1294d387df7SGuenter Roeck 	int err;
1304d387df7SGuenter Roeck 
1314d387df7SGuenter Roeck 	err = kstrtoul(buf, 10, &tmp);
1324d387df7SGuenter Roeck 	if (err)
1334d387df7SGuenter Roeck 		return err;
134add77c64SKrzysztof Helt 
135add77c64SKrzysztof Helt 	mutex_lock(&data->update_lock);
136add77c64SKrzysztof Helt 	data->analog_out = SENSORS_LIMIT(tmp, 0, 255);
137add77c64SKrzysztof Helt 	i2c_smbus_write_byte_data(client, THMC50_REG_ANALOG_OUT,
138add77c64SKrzysztof Helt 				  data->analog_out);
139add77c64SKrzysztof Helt 
140add77c64SKrzysztof Helt 	config = i2c_smbus_read_byte_data(client, THMC50_REG_CONF);
141add77c64SKrzysztof Helt 	if (data->analog_out == 0)
142add77c64SKrzysztof Helt 		config &= ~THMC50_REG_CONF_nFANOFF;
143add77c64SKrzysztof Helt 	else
144add77c64SKrzysztof Helt 		config |= THMC50_REG_CONF_nFANOFF;
145add77c64SKrzysztof Helt 	i2c_smbus_write_byte_data(client, THMC50_REG_CONF, config);
146add77c64SKrzysztof Helt 
147add77c64SKrzysztof Helt 	mutex_unlock(&data->update_lock);
148add77c64SKrzysztof Helt 	return count;
149add77c64SKrzysztof Helt }
150add77c64SKrzysztof Helt 
151add77c64SKrzysztof Helt /* There is only one PWM mode = DC */
152add77c64SKrzysztof Helt static ssize_t show_pwm_mode(struct device *dev, struct device_attribute *attr,
153add77c64SKrzysztof Helt 			     char *buf)
154add77c64SKrzysztof Helt {
155add77c64SKrzysztof Helt 	return sprintf(buf, "0\n");
156add77c64SKrzysztof Helt }
157add77c64SKrzysztof Helt 
158add77c64SKrzysztof Helt /* Temperatures */
159add77c64SKrzysztof Helt static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
160add77c64SKrzysztof Helt 			 char *buf)
161add77c64SKrzysztof Helt {
162add77c64SKrzysztof Helt 	int nr = to_sensor_dev_attr(attr)->index;
163add77c64SKrzysztof Helt 	struct thmc50_data *data = thmc50_update_device(dev);
164add77c64SKrzysztof Helt 	return sprintf(buf, "%d\n", data->temp_input[nr] * 1000);
165add77c64SKrzysztof Helt }
166add77c64SKrzysztof Helt 
167add77c64SKrzysztof Helt static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
168add77c64SKrzysztof Helt 			     char *buf)
169add77c64SKrzysztof Helt {
170add77c64SKrzysztof Helt 	int nr = to_sensor_dev_attr(attr)->index;
171add77c64SKrzysztof Helt 	struct thmc50_data *data = thmc50_update_device(dev);
172add77c64SKrzysztof Helt 	return sprintf(buf, "%d\n", data->temp_min[nr] * 1000);
173add77c64SKrzysztof Helt }
174add77c64SKrzysztof Helt 
175add77c64SKrzysztof Helt static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
176add77c64SKrzysztof Helt 			    const char *buf, size_t count)
177add77c64SKrzysztof Helt {
178add77c64SKrzysztof Helt 	int nr = to_sensor_dev_attr(attr)->index;
179add77c64SKrzysztof Helt 	struct i2c_client *client = to_i2c_client(dev);
180add77c64SKrzysztof Helt 	struct thmc50_data *data = i2c_get_clientdata(client);
1814d387df7SGuenter Roeck 	long val;
1824d387df7SGuenter Roeck 	int err;
1834d387df7SGuenter Roeck 
1844d387df7SGuenter Roeck 	err = kstrtol(buf, 10, &val);
1854d387df7SGuenter Roeck 	if (err)
1864d387df7SGuenter Roeck 		return err;
187add77c64SKrzysztof Helt 
188add77c64SKrzysztof Helt 	mutex_lock(&data->update_lock);
189add77c64SKrzysztof Helt 	data->temp_min[nr] = SENSORS_LIMIT(val / 1000, -128, 127);
190add77c64SKrzysztof Helt 	i2c_smbus_write_byte_data(client, THMC50_REG_TEMP_MIN[nr],
191add77c64SKrzysztof Helt 				  data->temp_min[nr]);
192add77c64SKrzysztof Helt 	mutex_unlock(&data->update_lock);
193add77c64SKrzysztof Helt 	return count;
194add77c64SKrzysztof Helt }
195add77c64SKrzysztof Helt 
196add77c64SKrzysztof Helt static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
197add77c64SKrzysztof Helt 			     char *buf)
198add77c64SKrzysztof Helt {
199add77c64SKrzysztof Helt 	int nr = to_sensor_dev_attr(attr)->index;
200add77c64SKrzysztof Helt 	struct thmc50_data *data = thmc50_update_device(dev);
201add77c64SKrzysztof Helt 	return sprintf(buf, "%d\n", data->temp_max[nr] * 1000);
202add77c64SKrzysztof Helt }
203add77c64SKrzysztof Helt 
204add77c64SKrzysztof Helt static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
205add77c64SKrzysztof Helt 			    const char *buf, size_t count)
206add77c64SKrzysztof Helt {
207add77c64SKrzysztof Helt 	int nr = to_sensor_dev_attr(attr)->index;
208add77c64SKrzysztof Helt 	struct i2c_client *client = to_i2c_client(dev);
209add77c64SKrzysztof Helt 	struct thmc50_data *data = i2c_get_clientdata(client);
2104d387df7SGuenter Roeck 	long val;
2114d387df7SGuenter Roeck 	int err;
2124d387df7SGuenter Roeck 
2134d387df7SGuenter Roeck 	err = kstrtol(buf, 10, &val);
2144d387df7SGuenter Roeck 	if (err)
2154d387df7SGuenter Roeck 		return err;
216add77c64SKrzysztof Helt 
217add77c64SKrzysztof Helt 	mutex_lock(&data->update_lock);
218add77c64SKrzysztof Helt 	data->temp_max[nr] = SENSORS_LIMIT(val / 1000, -128, 127);
219add77c64SKrzysztof Helt 	i2c_smbus_write_byte_data(client, THMC50_REG_TEMP_MAX[nr],
220add77c64SKrzysztof Helt 				  data->temp_max[nr]);
221add77c64SKrzysztof Helt 	mutex_unlock(&data->update_lock);
222add77c64SKrzysztof Helt 	return count;
223add77c64SKrzysztof Helt }
224add77c64SKrzysztof Helt 
22584f768c1SKrzysztof Helt static ssize_t show_temp_critical(struct device *dev,
22684f768c1SKrzysztof Helt 				  struct device_attribute *attr,
22784f768c1SKrzysztof Helt 				  char *buf)
22884f768c1SKrzysztof Helt {
22984f768c1SKrzysztof Helt 	int nr = to_sensor_dev_attr(attr)->index;
23084f768c1SKrzysztof Helt 	struct thmc50_data *data = thmc50_update_device(dev);
23184f768c1SKrzysztof Helt 	return sprintf(buf, "%d\n", data->temp_critical[nr] * 1000);
23284f768c1SKrzysztof Helt }
23384f768c1SKrzysztof Helt 
234dcf3b5fbSKrzysztof Helt static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
235dcf3b5fbSKrzysztof Helt 			  char *buf)
236dcf3b5fbSKrzysztof Helt {
237dcf3b5fbSKrzysztof Helt 	int index = to_sensor_dev_attr(attr)->index;
238dcf3b5fbSKrzysztof Helt 	struct thmc50_data *data = thmc50_update_device(dev);
239dcf3b5fbSKrzysztof Helt 
240dcf3b5fbSKrzysztof Helt 	return sprintf(buf, "%u\n", (data->alarms >> index) & 1);
241dcf3b5fbSKrzysztof Helt }
242dcf3b5fbSKrzysztof Helt 
243add77c64SKrzysztof Helt #define temp_reg(offset)						\
244add77c64SKrzysztof Helt static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp,	\
245add77c64SKrzysztof Helt 			NULL, offset - 1);				\
246add77c64SKrzysztof Helt static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR,	\
247add77c64SKrzysztof Helt 			show_temp_min, set_temp_min, offset - 1);	\
248add77c64SKrzysztof Helt static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR,	\
24984f768c1SKrzysztof Helt 			show_temp_max, set_temp_max, offset - 1);	\
25084f768c1SKrzysztof Helt static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IRUGO,			\
25184f768c1SKrzysztof Helt 			show_temp_critical, NULL, offset - 1);
252add77c64SKrzysztof Helt 
253add77c64SKrzysztof Helt temp_reg(1);
254add77c64SKrzysztof Helt temp_reg(2);
255add77c64SKrzysztof Helt temp_reg(3);
256add77c64SKrzysztof Helt 
257dcf3b5fbSKrzysztof Helt static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 0);
258dcf3b5fbSKrzysztof Helt static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
259dcf3b5fbSKrzysztof Helt static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 1);
260dcf3b5fbSKrzysztof Helt static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 7);
261dcf3b5fbSKrzysztof Helt static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 2);
262dcf3b5fbSKrzysztof Helt 
263add77c64SKrzysztof Helt static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_analog_out,
264add77c64SKrzysztof Helt 			  set_analog_out, 0);
265add77c64SKrzysztof Helt static SENSOR_DEVICE_ATTR(pwm1_mode, S_IRUGO, show_pwm_mode, NULL, 0);
266add77c64SKrzysztof Helt 
267add77c64SKrzysztof Helt static struct attribute *thmc50_attributes[] = {
268add77c64SKrzysztof Helt 	&sensor_dev_attr_temp1_max.dev_attr.attr,
269add77c64SKrzysztof Helt 	&sensor_dev_attr_temp1_min.dev_attr.attr,
270add77c64SKrzysztof Helt 	&sensor_dev_attr_temp1_input.dev_attr.attr,
27184f768c1SKrzysztof Helt 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
272dcf3b5fbSKrzysztof Helt 	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
273add77c64SKrzysztof Helt 	&sensor_dev_attr_temp2_max.dev_attr.attr,
274add77c64SKrzysztof Helt 	&sensor_dev_attr_temp2_min.dev_attr.attr,
275add77c64SKrzysztof Helt 	&sensor_dev_attr_temp2_input.dev_attr.attr,
27684f768c1SKrzysztof Helt 	&sensor_dev_attr_temp2_crit.dev_attr.attr,
277dcf3b5fbSKrzysztof Helt 	&sensor_dev_attr_temp2_alarm.dev_attr.attr,
278dcf3b5fbSKrzysztof Helt 	&sensor_dev_attr_temp2_fault.dev_attr.attr,
279add77c64SKrzysztof Helt 	&sensor_dev_attr_pwm1.dev_attr.attr,
280add77c64SKrzysztof Helt 	&sensor_dev_attr_pwm1_mode.dev_attr.attr,
281add77c64SKrzysztof Helt 	NULL
282add77c64SKrzysztof Helt };
283add77c64SKrzysztof Helt 
284add77c64SKrzysztof Helt static const struct attribute_group thmc50_group = {
285add77c64SKrzysztof Helt 	.attrs = thmc50_attributes,
286add77c64SKrzysztof Helt };
287add77c64SKrzysztof Helt 
288add77c64SKrzysztof Helt /* for ADM1022 3rd temperature mode */
289894c00cfSJean Delvare static struct attribute *temp3_attributes[] = {
290add77c64SKrzysztof Helt 	&sensor_dev_attr_temp3_max.dev_attr.attr,
291add77c64SKrzysztof Helt 	&sensor_dev_attr_temp3_min.dev_attr.attr,
292add77c64SKrzysztof Helt 	&sensor_dev_attr_temp3_input.dev_attr.attr,
29384f768c1SKrzysztof Helt 	&sensor_dev_attr_temp3_crit.dev_attr.attr,
294dcf3b5fbSKrzysztof Helt 	&sensor_dev_attr_temp3_alarm.dev_attr.attr,
295dcf3b5fbSKrzysztof Helt 	&sensor_dev_attr_temp3_fault.dev_attr.attr,
296add77c64SKrzysztof Helt 	NULL
297add77c64SKrzysztof Helt };
298add77c64SKrzysztof Helt 
299894c00cfSJean Delvare static const struct attribute_group temp3_group = {
300894c00cfSJean Delvare 	.attrs = temp3_attributes,
301add77c64SKrzysztof Helt };
302add77c64SKrzysztof Helt 
303ccf37488SJean Delvare /* Return 0 if detection is successful, -ENODEV otherwise */
304310ec792SJean Delvare static int thmc50_detect(struct i2c_client *client,
305ccf37488SJean Delvare 			 struct i2c_board_info *info)
306add77c64SKrzysztof Helt {
307add77c64SKrzysztof Helt 	unsigned company;
308add77c64SKrzysztof Helt 	unsigned revision;
309add77c64SKrzysztof Helt 	unsigned config;
310ccf37488SJean Delvare 	struct i2c_adapter *adapter = client->adapter;
311cc28a610SJean Delvare 	const char *type_name;
312add77c64SKrzysztof Helt 
313add77c64SKrzysztof Helt 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
314add77c64SKrzysztof Helt 		pr_debug("thmc50: detect failed, "
315add77c64SKrzysztof Helt 			 "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 
364*bd91d3baSGuenter Roeck 	data = devm_kzalloc(&client->dev, sizeof(struct thmc50_data),
365*bd91d3baSGuenter Roeck 			    GFP_KERNEL);
366*bd91d3baSGuenter Roeck 	if (!data)
367*bd91d3baSGuenter 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)
378*bd91d3baSGuenter 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