summaryrefslogtreecommitdiff
path: root/drivers/hwmon/nct1008.c
blob: 72fa9b11642fd3993bba33d4ae574286ffebc38c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
 * drivers/hwmon/nct1008.c
 *
 * Temperature Sensor driver for NCT1008 Temperature Monitor chip
 * manufactured by ON Semiconductors (www.onsemi.com).
 *
 * Copyright (c) 2010, NVIDIA Corporation.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <linux/module.h>
#include <linux/i2c.h>
#include <linux/i2c/nct1008.h>
#include <linux/hwmon.h>
#include <linux/slab.h>
#include <linux/err.h>
#include <linux/miscdevice.h>
#include <linux/fs.h>
#include <linux/delay.h>

#define NCT1008_LOCAL_TEMP_RD			0x00
#define NCT1008_CONFIG_RD			0x03
#define NCT1008_MFR_ID_RD			0xFE

#define NCT1008_CONFIG_WR			0x09
 #define NCT1008_CONFIG_ALERT_DISABLE		0x80
 #define NCT1008_CONFIG_RUN_STANDBY		0x40
 #define NCT1008_CONFIG_ALERT_THERM2		0x20
 #define NCT1008_CONFIG_ENABLE_EXTENDED		0x04

#define NCT1008_CONV_RATE_WR			0x0A
#define NCT1008_LOCAL_THERM_LIMIT_WR		0x20

#define DRIVER_NAME "nct1008"

struct nct1008_data {
	struct device *hwmon_dev;
	struct i2c_client *client;
	struct nct1008_platform_data plat_data;
};

static ssize_t nct1008_show_temp(struct device *dev,
	struct device_attribute *attr, char *buf)
{
	struct i2c_client *client = to_i2c_client(dev);
	signed int temp_value = 0;
	u8 data = 0;

	if (!dev || !buf || !attr)
		return -EINVAL;

	data = i2c_smbus_read_byte_data(client, NCT1008_LOCAL_TEMP_RD);
	if (data < 0) {
		dev_err(&client->dev, "%s: failed to read "
			"temperature\n", __func__);
		return -EINVAL;
	}

	temp_value = (signed int)data;
	return sprintf(buf, "%d", temp_value);
}

static DEVICE_ATTR(temperature, S_IRUGO, nct1008_show_temp, NULL);

static struct attribute *nct1008_attributes[] = {
	&dev_attr_temperature.attr,
	NULL
};

static const struct attribute_group nct1008_attr_group = {
	.attrs = nct1008_attributes,
};

static int __devinit nct1008_probe(struct i2c_client *client,
		const struct i2c_device_id *id)
{
	struct nct1008_data *pdata;
	int err;
	u8 data = 0;

	if (client->dev.platform_data == NULL) {
		dev_err(&client->dev, "platform data is NULL, exiting\n");
		return -ENODEV;
	}

	pdata = kzalloc(sizeof(struct nct1008_data), GFP_KERNEL);
	if (!pdata) {
		dev_err(&client->dev, "%s: failed to allocate "
			"device\n", __func__);
		return -ENOMEM;
	}

	pdata->client = client;
	i2c_set_clientdata(client, pdata);

	memcpy(&pdata->plat_data, client->dev.platform_data,
		sizeof(struct nct1008_platform_data));

	data = i2c_smbus_read_byte_data(client, NCT1008_MFR_ID_RD);
	if (data < 0) {
		dev_err(&client->dev, "%s: failed to read manufacturer "
			"id\n", __func__);
		err = data;
		goto fail_alloc;
	}
	dev_info(&client->dev, "%s:0x%x chip found\n", client->name, data);

	/* set conversion rate (conv/sec) */
	data = pdata->plat_data.conv_rate;
	err = i2c_smbus_write_byte_data(client, NCT1008_CONV_RATE_WR, data);
	if (err < 0) {
		dev_err(&client->dev, "%s: failed to set conversion "
			"rate\n", __func__);
		goto fail_alloc;
	}

	data = i2c_smbus_read_byte_data(client, NCT1008_CONFIG_RD);
	if (data < 0) {
		dev_err(&client->dev, "%s: failed to read config\n", __func__);
		err = data;
		goto fail_alloc;
	}

	/* set config params */
	data |= pdata->plat_data.config;
	err = i2c_smbus_write_byte_data(client, NCT1008_CONFIG_WR, data);
	if (err < 0) {
		dev_err(&client->dev, "%s: failed to set config\n", __func__);
		goto fail_alloc;
	}

	/* set cpu shutdown threshold */
	data = pdata->plat_data.thermal_threshold;
	err = i2c_smbus_write_byte_data(client, NCT1008_LOCAL_THERM_LIMIT_WR, data);
	if (err < 0) {
		dev_err(&client->dev,
				"%s: failed to set THERM# limit\n", __func__);
		goto fail_alloc;
	}

	pdata->hwmon_dev = hwmon_device_register(&client->dev);
	if (IS_ERR(pdata->hwmon_dev)) {
		err = PTR_ERR(pdata->hwmon_dev);
		dev_err(&client->dev, "%s: hwmon_device_register "
			"failed\n", __func__);
		goto fail_alloc;
	}

	/* register sysfs hooks */
	err = sysfs_create_group(&client->dev.kobj, &nct1008_attr_group);
	if (err < 0)
		goto fail_sys;

	dev_info(&client->dev, "%s: initialized\n", __func__);
	return 0;

fail_sys:
	hwmon_device_unregister(pdata->hwmon_dev);
fail_alloc:
	kfree(pdata);
	return err;
}

static int __devexit nct1008_remove(struct i2c_client *client)
{
	struct nct1008_data *pdata = i2c_get_clientdata(client);

	if (!pdata)
		return -EINVAL;

	hwmon_device_unregister(pdata->hwmon_dev);
	kfree(pdata);
	return 0;
}

#ifdef CONFIG_PM
static int nct1008_suspend(struct i2c_client *client, pm_message_t state)
{
	u8 config;
	int err;

	config = i2c_smbus_read_byte_data(client, NCT1008_CONFIG_RD);
	if (config < 0) {
		dev_err(&client->dev, "%s: failed to read config\n", __func__);
		return -EIO;
	}

	/* take device to standby state */
	config |= NCT1008_CONFIG_RUN_STANDBY;

	err = i2c_smbus_write_byte_data(client, NCT1008_CONFIG_WR, config);
	if (err < 0) {
		dev_err(&client->dev, "%s: failed to set config\n", __func__);
		return -EIO;
	}

	return 0;
}

static int nct1008_resume(struct i2c_client *client)
{
	u8 config = 0;
	int err;

	config = i2c_smbus_read_byte_data(client, NCT1008_CONFIG_RD);
	if (config < 0) {
		dev_err(&client->dev, "%s: failed to read config\n", __func__);
		return -EIO;
	}

	/* take device out of standby state */
	config &= ~NCT1008_CONFIG_RUN_STANDBY;

	err = i2c_smbus_write_byte_data(client, NCT1008_CONFIG_WR, config);
	if (err < 0) {
		dev_err(&client->dev, "%s: failed to set config\n", __func__);
		return -EIO;
	}
	return 0;
}
#endif

static const struct i2c_device_id nct1008_id[] = {
	{ DRIVER_NAME, 0 },
	{ }
};
MODULE_DEVICE_TABLE(i2c, nct1008_id);

static struct i2c_driver nct1008_driver = {
	.class		= I2C_CLASS_HWMON,
	.driver = {
		.name	= DRIVER_NAME,
	},
	.probe		= nct1008_probe,
	.remove		= __devexit_p(nct1008_remove),
	.id_table	= nct1008_id,
#ifdef CONFIG_PM
	.suspend = nct1008_suspend,
	.resume = nct1008_resume,
#endif
};

static int __init nct1008_init(void)
{
	return i2c_add_driver(&nct1008_driver);
}

static void __exit nct1008_exit(void)
{
	i2c_del_driver(&nct1008_driver);
}

module_init (nct1008_init);
module_exit (nct1008_exit);

#define DRIVER_DESC "NCT1008 temperature sensor driver"
#define DRIVER_LICENSE "GPL"

MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE(DRIVER_LICENSE);