tmp401.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. /* tmp401.c
  2. *
  3. * Copyright (C) 2007,2008 Hans de Goede <hdegoede@redhat.com>
  4. * Preliminary tmp411 support by:
  5. * Gabriel Konat, Sander Leget, Wouter Willems
  6. * Copyright (C) 2009 Andre Prendel <andre.prendel@gmx.de>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. /*
  23. * Driver for the Texas Instruments TMP401 SMBUS temperature sensor IC.
  24. *
  25. * Note this IC is in some aspect similar to the LM90, but it has quite a
  26. * few differences too, for example the local temp has a higher resolution
  27. * and thus has 16 bits registers for its value and limit instead of 8 bits.
  28. */
  29. #include <linux/module.h>
  30. #include <linux/init.h>
  31. #include <linux/slab.h>
  32. #include <linux/jiffies.h>
  33. #include <linux/i2c.h>
  34. #include <linux/hwmon.h>
  35. #include <linux/hwmon-sysfs.h>
  36. #include <linux/err.h>
  37. #include <linux/mutex.h>
  38. #include <linux/sysfs.h>
  39. /* Addresses to scan */
  40. static const unsigned short normal_i2c[] = { 0x4c, I2C_CLIENT_END };
  41. enum chips { tmp401, tmp411 };
  42. /*
  43. * The TMP401 registers, note some registers have different addresses for
  44. * reading and writing
  45. */
  46. #define TMP401_STATUS 0x02
  47. #define TMP401_CONFIG_READ 0x03
  48. #define TMP401_CONFIG_WRITE 0x09
  49. #define TMP401_CONVERSION_RATE_READ 0x04
  50. #define TMP401_CONVERSION_RATE_WRITE 0x0A
  51. #define TMP401_TEMP_CRIT_HYST 0x21
  52. #define TMP401_CONSECUTIVE_ALERT 0x22
  53. #define TMP401_MANUFACTURER_ID_REG 0xFE
  54. #define TMP401_DEVICE_ID_REG 0xFF
  55. #define TMP411_N_FACTOR_REG 0x18
  56. static const u8 TMP401_TEMP_MSB[2] = { 0x00, 0x01 };
  57. static const u8 TMP401_TEMP_LSB[2] = { 0x15, 0x10 };
  58. static const u8 TMP401_TEMP_LOW_LIMIT_MSB_READ[2] = { 0x06, 0x08 };
  59. static const u8 TMP401_TEMP_LOW_LIMIT_MSB_WRITE[2] = { 0x0C, 0x0E };
  60. static const u8 TMP401_TEMP_LOW_LIMIT_LSB[2] = { 0x17, 0x14 };
  61. static const u8 TMP401_TEMP_HIGH_LIMIT_MSB_READ[2] = { 0x05, 0x07 };
  62. static const u8 TMP401_TEMP_HIGH_LIMIT_MSB_WRITE[2] = { 0x0B, 0x0D };
  63. static const u8 TMP401_TEMP_HIGH_LIMIT_LSB[2] = { 0x16, 0x13 };
  64. /* These are called the THERM limit / hysteresis / mask in the datasheet */
  65. static const u8 TMP401_TEMP_CRIT_LIMIT[2] = { 0x20, 0x19 };
  66. static const u8 TMP411_TEMP_LOWEST_MSB[2] = { 0x30, 0x34 };
  67. static const u8 TMP411_TEMP_LOWEST_LSB[2] = { 0x31, 0x35 };
  68. static const u8 TMP411_TEMP_HIGHEST_MSB[2] = { 0x32, 0x36 };
  69. static const u8 TMP411_TEMP_HIGHEST_LSB[2] = { 0x33, 0x37 };
  70. /* Flags */
  71. #define TMP401_CONFIG_RANGE 0x04
  72. #define TMP401_CONFIG_SHUTDOWN 0x40
  73. #define TMP401_STATUS_LOCAL_CRIT 0x01
  74. #define TMP401_STATUS_REMOTE_CRIT 0x02
  75. #define TMP401_STATUS_REMOTE_OPEN 0x04
  76. #define TMP401_STATUS_REMOTE_LOW 0x08
  77. #define TMP401_STATUS_REMOTE_HIGH 0x10
  78. #define TMP401_STATUS_LOCAL_LOW 0x20
  79. #define TMP401_STATUS_LOCAL_HIGH 0x40
  80. /* Manufacturer / Device ID's */
  81. #define TMP401_MANUFACTURER_ID 0x55
  82. #define TMP401_DEVICE_ID 0x11
  83. #define TMP411_DEVICE_ID 0x12
  84. /*
  85. * Functions declarations
  86. */
  87. static int tmp401_probe(struct i2c_client *client,
  88. const struct i2c_device_id *id);
  89. static int tmp401_detect(struct i2c_client *client,
  90. struct i2c_board_info *info);
  91. static int tmp401_remove(struct i2c_client *client);
  92. static struct tmp401_data *tmp401_update_device(struct device *dev);
  93. /*
  94. * Driver data (common to all clients)
  95. */
  96. static const struct i2c_device_id tmp401_id[] = {
  97. { "tmp401", tmp401 },
  98. { "tmp411", tmp411 },
  99. { }
  100. };
  101. MODULE_DEVICE_TABLE(i2c, tmp401_id);
  102. static struct i2c_driver tmp401_driver = {
  103. .class = I2C_CLASS_HWMON,
  104. .driver = {
  105. .name = "tmp401",
  106. },
  107. .probe = tmp401_probe,
  108. .remove = tmp401_remove,
  109. .id_table = tmp401_id,
  110. .detect = tmp401_detect,
  111. .address_list = normal_i2c,
  112. };
  113. /*
  114. * Client data (each client gets its own)
  115. */
  116. struct tmp401_data {
  117. struct device *hwmon_dev;
  118. struct mutex update_lock;
  119. char valid; /* zero until following fields are valid */
  120. unsigned long last_updated; /* in jiffies */
  121. int kind;
  122. /* register values */
  123. u8 status;
  124. u8 config;
  125. u16 temp[2];
  126. u16 temp_low[2];
  127. u16 temp_high[2];
  128. u8 temp_crit[2];
  129. u8 temp_crit_hyst;
  130. u16 temp_lowest[2];
  131. u16 temp_highest[2];
  132. };
  133. /*
  134. * Sysfs attr show / store functions
  135. */
  136. static int tmp401_register_to_temp(u16 reg, u8 config)
  137. {
  138. int temp = reg;
  139. if (config & TMP401_CONFIG_RANGE)
  140. temp -= 64 * 256;
  141. return (temp * 625 + 80) / 160;
  142. }
  143. static u16 tmp401_temp_to_register(long temp, u8 config)
  144. {
  145. if (config & TMP401_CONFIG_RANGE) {
  146. temp = SENSORS_LIMIT(temp, -64000, 191000);
  147. temp += 64000;
  148. } else
  149. temp = SENSORS_LIMIT(temp, 0, 127000);
  150. return (temp * 160 + 312) / 625;
  151. }
  152. static int tmp401_crit_register_to_temp(u8 reg, u8 config)
  153. {
  154. int temp = reg;
  155. if (config & TMP401_CONFIG_RANGE)
  156. temp -= 64;
  157. return temp * 1000;
  158. }
  159. static u8 tmp401_crit_temp_to_register(long temp, u8 config)
  160. {
  161. if (config & TMP401_CONFIG_RANGE) {
  162. temp = SENSORS_LIMIT(temp, -64000, 191000);
  163. temp += 64000;
  164. } else
  165. temp = SENSORS_LIMIT(temp, 0, 127000);
  166. return (temp + 500) / 1000;
  167. }
  168. static ssize_t show_temp_value(struct device *dev,
  169. struct device_attribute *devattr, char *buf)
  170. {
  171. int index = to_sensor_dev_attr(devattr)->index;
  172. struct tmp401_data *data = tmp401_update_device(dev);
  173. return sprintf(buf, "%d\n",
  174. tmp401_register_to_temp(data->temp[index], data->config));
  175. }
  176. static ssize_t show_temp_min(struct device *dev,
  177. struct device_attribute *devattr, char *buf)
  178. {
  179. int index = to_sensor_dev_attr(devattr)->index;
  180. struct tmp401_data *data = tmp401_update_device(dev);
  181. return sprintf(buf, "%d\n",
  182. tmp401_register_to_temp(data->temp_low[index], data->config));
  183. }
  184. static ssize_t show_temp_max(struct device *dev,
  185. struct device_attribute *devattr, char *buf)
  186. {
  187. int index = to_sensor_dev_attr(devattr)->index;
  188. struct tmp401_data *data = tmp401_update_device(dev);
  189. return sprintf(buf, "%d\n",
  190. tmp401_register_to_temp(data->temp_high[index], data->config));
  191. }
  192. static ssize_t show_temp_crit(struct device *dev,
  193. struct device_attribute *devattr, char *buf)
  194. {
  195. int index = to_sensor_dev_attr(devattr)->index;
  196. struct tmp401_data *data = tmp401_update_device(dev);
  197. return sprintf(buf, "%d\n",
  198. tmp401_crit_register_to_temp(data->temp_crit[index],
  199. data->config));
  200. }
  201. static ssize_t show_temp_crit_hyst(struct device *dev,
  202. struct device_attribute *devattr, char *buf)
  203. {
  204. int temp, index = to_sensor_dev_attr(devattr)->index;
  205. struct tmp401_data *data = tmp401_update_device(dev);
  206. mutex_lock(&data->update_lock);
  207. temp = tmp401_crit_register_to_temp(data->temp_crit[index],
  208. data->config);
  209. temp -= data->temp_crit_hyst * 1000;
  210. mutex_unlock(&data->update_lock);
  211. return sprintf(buf, "%d\n", temp);
  212. }
  213. static ssize_t show_temp_lowest(struct device *dev,
  214. struct device_attribute *devattr, char *buf)
  215. {
  216. int index = to_sensor_dev_attr(devattr)->index;
  217. struct tmp401_data *data = tmp401_update_device(dev);
  218. return sprintf(buf, "%d\n",
  219. tmp401_register_to_temp(data->temp_lowest[index],
  220. data->config));
  221. }
  222. static ssize_t show_temp_highest(struct device *dev,
  223. struct device_attribute *devattr, char *buf)
  224. {
  225. int index = to_sensor_dev_attr(devattr)->index;
  226. struct tmp401_data *data = tmp401_update_device(dev);
  227. return sprintf(buf, "%d\n",
  228. tmp401_register_to_temp(data->temp_highest[index],
  229. data->config));
  230. }
  231. static ssize_t show_status(struct device *dev,
  232. struct device_attribute *devattr, char *buf)
  233. {
  234. int mask = to_sensor_dev_attr(devattr)->index;
  235. struct tmp401_data *data = tmp401_update_device(dev);
  236. if (data->status & mask)
  237. return sprintf(buf, "1\n");
  238. else
  239. return sprintf(buf, "0\n");
  240. }
  241. static ssize_t store_temp_min(struct device *dev, struct device_attribute
  242. *devattr, const char *buf, size_t count)
  243. {
  244. int index = to_sensor_dev_attr(devattr)->index;
  245. struct tmp401_data *data = tmp401_update_device(dev);
  246. long val;
  247. u16 reg;
  248. if (strict_strtol(buf, 10, &val))
  249. return -EINVAL;
  250. reg = tmp401_temp_to_register(val, data->config);
  251. mutex_lock(&data->update_lock);
  252. i2c_smbus_write_byte_data(to_i2c_client(dev),
  253. TMP401_TEMP_LOW_LIMIT_MSB_WRITE[index], reg >> 8);
  254. i2c_smbus_write_byte_data(to_i2c_client(dev),
  255. TMP401_TEMP_LOW_LIMIT_LSB[index], reg & 0xFF);
  256. data->temp_low[index] = reg;
  257. mutex_unlock(&data->update_lock);
  258. return count;
  259. }
  260. static ssize_t store_temp_max(struct device *dev, struct device_attribute
  261. *devattr, const char *buf, size_t count)
  262. {
  263. int index = to_sensor_dev_attr(devattr)->index;
  264. struct tmp401_data *data = tmp401_update_device(dev);
  265. long val;
  266. u16 reg;
  267. if (strict_strtol(buf, 10, &val))
  268. return -EINVAL;
  269. reg = tmp401_temp_to_register(val, data->config);
  270. mutex_lock(&data->update_lock);
  271. i2c_smbus_write_byte_data(to_i2c_client(dev),
  272. TMP401_TEMP_HIGH_LIMIT_MSB_WRITE[index], reg >> 8);
  273. i2c_smbus_write_byte_data(to_i2c_client(dev),
  274. TMP401_TEMP_HIGH_LIMIT_LSB[index], reg & 0xFF);
  275. data->temp_high[index] = reg;
  276. mutex_unlock(&data->update_lock);
  277. return count;
  278. }
  279. static ssize_t store_temp_crit(struct device *dev, struct device_attribute
  280. *devattr, const char *buf, size_t count)
  281. {
  282. int index = to_sensor_dev_attr(devattr)->index;
  283. struct tmp401_data *data = tmp401_update_device(dev);
  284. long val;
  285. u8 reg;
  286. if (strict_strtol(buf, 10, &val))
  287. return -EINVAL;
  288. reg = tmp401_crit_temp_to_register(val, data->config);
  289. mutex_lock(&data->update_lock);
  290. i2c_smbus_write_byte_data(to_i2c_client(dev),
  291. TMP401_TEMP_CRIT_LIMIT[index], reg);
  292. data->temp_crit[index] = reg;
  293. mutex_unlock(&data->update_lock);
  294. return count;
  295. }
  296. static ssize_t store_temp_crit_hyst(struct device *dev, struct device_attribute
  297. *devattr, const char *buf, size_t count)
  298. {
  299. int temp, index = to_sensor_dev_attr(devattr)->index;
  300. struct tmp401_data *data = tmp401_update_device(dev);
  301. long val;
  302. u8 reg;
  303. if (strict_strtol(buf, 10, &val))
  304. return -EINVAL;
  305. if (data->config & TMP401_CONFIG_RANGE)
  306. val = SENSORS_LIMIT(val, -64000, 191000);
  307. else
  308. val = SENSORS_LIMIT(val, 0, 127000);
  309. mutex_lock(&data->update_lock);
  310. temp = tmp401_crit_register_to_temp(data->temp_crit[index],
  311. data->config);
  312. val = SENSORS_LIMIT(val, temp - 255000, temp);
  313. reg = ((temp - val) + 500) / 1000;
  314. i2c_smbus_write_byte_data(to_i2c_client(dev),
  315. TMP401_TEMP_CRIT_HYST, reg);
  316. data->temp_crit_hyst = reg;
  317. mutex_unlock(&data->update_lock);
  318. return count;
  319. }
  320. /*
  321. * Resets the historical measurements of minimum and maximum temperatures.
  322. * This is done by writing any value to any of the minimum/maximum registers
  323. * (0x30-0x37).
  324. */
  325. static ssize_t reset_temp_history(struct device *dev,
  326. struct device_attribute *devattr, const char *buf, size_t count)
  327. {
  328. long val;
  329. if (strict_strtol(buf, 10, &val))
  330. return -EINVAL;
  331. if (val != 1) {
  332. dev_err(dev, "temp_reset_history value %ld not"
  333. " supported. Use 1 to reset the history!\n", val);
  334. return -EINVAL;
  335. }
  336. i2c_smbus_write_byte_data(to_i2c_client(dev),
  337. TMP411_TEMP_LOWEST_MSB[0], val);
  338. return count;
  339. }
  340. static struct sensor_device_attribute tmp401_attr[] = {
  341. SENSOR_ATTR(temp1_input, 0444, show_temp_value, NULL, 0),
  342. SENSOR_ATTR(temp1_min, 0644, show_temp_min, store_temp_min, 0),
  343. SENSOR_ATTR(temp1_max, 0644, show_temp_max, store_temp_max, 0),
  344. SENSOR_ATTR(temp1_crit, 0644, show_temp_crit, store_temp_crit, 0),
  345. SENSOR_ATTR(temp1_crit_hyst, 0644, show_temp_crit_hyst,
  346. store_temp_crit_hyst, 0),
  347. SENSOR_ATTR(temp1_min_alarm, 0444, show_status, NULL,
  348. TMP401_STATUS_LOCAL_LOW),
  349. SENSOR_ATTR(temp1_max_alarm, 0444, show_status, NULL,
  350. TMP401_STATUS_LOCAL_HIGH),
  351. SENSOR_ATTR(temp1_crit_alarm, 0444, show_status, NULL,
  352. TMP401_STATUS_LOCAL_CRIT),
  353. SENSOR_ATTR(temp2_input, 0444, show_temp_value, NULL, 1),
  354. SENSOR_ATTR(temp2_min, 0644, show_temp_min, store_temp_min, 1),
  355. SENSOR_ATTR(temp2_max, 0644, show_temp_max, store_temp_max, 1),
  356. SENSOR_ATTR(temp2_crit, 0644, show_temp_crit, store_temp_crit, 1),
  357. SENSOR_ATTR(temp2_crit_hyst, 0444, show_temp_crit_hyst, NULL, 1),
  358. SENSOR_ATTR(temp2_fault, 0444, show_status, NULL,
  359. TMP401_STATUS_REMOTE_OPEN),
  360. SENSOR_ATTR(temp2_min_alarm, 0444, show_status, NULL,
  361. TMP401_STATUS_REMOTE_LOW),
  362. SENSOR_ATTR(temp2_max_alarm, 0444, show_status, NULL,
  363. TMP401_STATUS_REMOTE_HIGH),
  364. SENSOR_ATTR(temp2_crit_alarm, 0444, show_status, NULL,
  365. TMP401_STATUS_REMOTE_CRIT),
  366. };
  367. /*
  368. * Additional features of the TMP411 chip.
  369. * The TMP411 stores the minimum and maximum
  370. * temperature measured since power-on, chip-reset, or
  371. * minimum and maximum register reset for both the local
  372. * and remote channels.
  373. */
  374. static struct sensor_device_attribute tmp411_attr[] = {
  375. SENSOR_ATTR(temp1_highest, 0444, show_temp_highest, NULL, 0),
  376. SENSOR_ATTR(temp1_lowest, 0444, show_temp_lowest, NULL, 0),
  377. SENSOR_ATTR(temp2_highest, 0444, show_temp_highest, NULL, 1),
  378. SENSOR_ATTR(temp2_lowest, 0444, show_temp_lowest, NULL, 1),
  379. SENSOR_ATTR(temp_reset_history, 0200, NULL, reset_temp_history, 0),
  380. };
  381. /*
  382. * Begin non sysfs callback code (aka Real code)
  383. */
  384. static void tmp401_init_client(struct i2c_client *client)
  385. {
  386. int config, config_orig;
  387. /* Set the conversion rate to 2 Hz */
  388. i2c_smbus_write_byte_data(client, TMP401_CONVERSION_RATE_WRITE, 5);
  389. /* Start conversions (disable shutdown if necessary) */
  390. config = i2c_smbus_read_byte_data(client, TMP401_CONFIG_READ);
  391. if (config < 0) {
  392. dev_warn(&client->dev, "Initialization failed!\n");
  393. return;
  394. }
  395. config_orig = config;
  396. config &= ~TMP401_CONFIG_SHUTDOWN;
  397. if (config != config_orig)
  398. i2c_smbus_write_byte_data(client, TMP401_CONFIG_WRITE, config);
  399. }
  400. static int tmp401_detect(struct i2c_client *client,
  401. struct i2c_board_info *info)
  402. {
  403. enum chips kind;
  404. struct i2c_adapter *adapter = client->adapter;
  405. u8 reg;
  406. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  407. return -ENODEV;
  408. /* Detect and identify the chip */
  409. reg = i2c_smbus_read_byte_data(client, TMP401_MANUFACTURER_ID_REG);
  410. if (reg != TMP401_MANUFACTURER_ID)
  411. return -ENODEV;
  412. reg = i2c_smbus_read_byte_data(client, TMP401_DEVICE_ID_REG);
  413. switch (reg) {
  414. case TMP401_DEVICE_ID:
  415. kind = tmp401;
  416. break;
  417. case TMP411_DEVICE_ID:
  418. kind = tmp411;
  419. break;
  420. default:
  421. return -ENODEV;
  422. }
  423. reg = i2c_smbus_read_byte_data(client, TMP401_CONFIG_READ);
  424. if (reg & 0x1b)
  425. return -ENODEV;
  426. reg = i2c_smbus_read_byte_data(client, TMP401_CONVERSION_RATE_READ);
  427. /* Datasheet says: 0x1-0x6 */
  428. if (reg > 15)
  429. return -ENODEV;
  430. strlcpy(info->type, tmp401_id[kind - 1].name, I2C_NAME_SIZE);
  431. return 0;
  432. }
  433. static int tmp401_probe(struct i2c_client *client,
  434. const struct i2c_device_id *id)
  435. {
  436. int i, err = 0;
  437. struct tmp401_data *data;
  438. const char *names[] = { "TMP401", "TMP411" };
  439. data = kzalloc(sizeof(struct tmp401_data), GFP_KERNEL);
  440. if (!data)
  441. return -ENOMEM;
  442. i2c_set_clientdata(client, data);
  443. mutex_init(&data->update_lock);
  444. data->kind = id->driver_data;
  445. /* Initialize the TMP401 chip */
  446. tmp401_init_client(client);
  447. /* Register sysfs hooks */
  448. for (i = 0; i < ARRAY_SIZE(tmp401_attr); i++) {
  449. err = device_create_file(&client->dev,
  450. &tmp401_attr[i].dev_attr);
  451. if (err)
  452. goto exit_remove;
  453. }
  454. /* Register aditional tmp411 sysfs hooks */
  455. if (data->kind == tmp411) {
  456. for (i = 0; i < ARRAY_SIZE(tmp411_attr); i++) {
  457. err = device_create_file(&client->dev,
  458. &tmp411_attr[i].dev_attr);
  459. if (err)
  460. goto exit_remove;
  461. }
  462. }
  463. data->hwmon_dev = hwmon_device_register(&client->dev);
  464. if (IS_ERR(data->hwmon_dev)) {
  465. err = PTR_ERR(data->hwmon_dev);
  466. data->hwmon_dev = NULL;
  467. goto exit_remove;
  468. }
  469. dev_info(&client->dev, "Detected TI %s chip\n",
  470. names[data->kind - 1]);
  471. return 0;
  472. exit_remove:
  473. tmp401_remove(client); /* will also free data for us */
  474. return err;
  475. }
  476. static int tmp401_remove(struct i2c_client *client)
  477. {
  478. struct tmp401_data *data = i2c_get_clientdata(client);
  479. int i;
  480. if (data->hwmon_dev)
  481. hwmon_device_unregister(data->hwmon_dev);
  482. for (i = 0; i < ARRAY_SIZE(tmp401_attr); i++)
  483. device_remove_file(&client->dev, &tmp401_attr[i].dev_attr);
  484. if (data->kind == tmp411) {
  485. for (i = 0; i < ARRAY_SIZE(tmp411_attr); i++)
  486. device_remove_file(&client->dev,
  487. &tmp411_attr[i].dev_attr);
  488. }
  489. kfree(data);
  490. return 0;
  491. }
  492. static struct tmp401_data *tmp401_update_device_reg16(
  493. struct i2c_client *client, struct tmp401_data *data)
  494. {
  495. int i;
  496. for (i = 0; i < 2; i++) {
  497. /*
  498. * High byte must be read first immediately followed
  499. * by the low byte
  500. */
  501. data->temp[i] = i2c_smbus_read_byte_data(client,
  502. TMP401_TEMP_MSB[i]) << 8;
  503. data->temp[i] |= i2c_smbus_read_byte_data(client,
  504. TMP401_TEMP_LSB[i]);
  505. data->temp_low[i] = i2c_smbus_read_byte_data(client,
  506. TMP401_TEMP_LOW_LIMIT_MSB_READ[i]) << 8;
  507. data->temp_low[i] |= i2c_smbus_read_byte_data(client,
  508. TMP401_TEMP_LOW_LIMIT_LSB[i]);
  509. data->temp_high[i] = i2c_smbus_read_byte_data(client,
  510. TMP401_TEMP_HIGH_LIMIT_MSB_READ[i]) << 8;
  511. data->temp_high[i] |= i2c_smbus_read_byte_data(client,
  512. TMP401_TEMP_HIGH_LIMIT_LSB[i]);
  513. data->temp_crit[i] = i2c_smbus_read_byte_data(client,
  514. TMP401_TEMP_CRIT_LIMIT[i]);
  515. if (data->kind == tmp411) {
  516. data->temp_lowest[i] = i2c_smbus_read_byte_data(client,
  517. TMP411_TEMP_LOWEST_MSB[i]) << 8;
  518. data->temp_lowest[i] |= i2c_smbus_read_byte_data(
  519. client, TMP411_TEMP_LOWEST_LSB[i]);
  520. data->temp_highest[i] = i2c_smbus_read_byte_data(
  521. client, TMP411_TEMP_HIGHEST_MSB[i]) << 8;
  522. data->temp_highest[i] |= i2c_smbus_read_byte_data(
  523. client, TMP411_TEMP_HIGHEST_LSB[i]);
  524. }
  525. }
  526. return data;
  527. }
  528. static struct tmp401_data *tmp401_update_device(struct device *dev)
  529. {
  530. struct i2c_client *client = to_i2c_client(dev);
  531. struct tmp401_data *data = i2c_get_clientdata(client);
  532. mutex_lock(&data->update_lock);
  533. if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
  534. data->status = i2c_smbus_read_byte_data(client, TMP401_STATUS);
  535. data->config = i2c_smbus_read_byte_data(client,
  536. TMP401_CONFIG_READ);
  537. tmp401_update_device_reg16(client, data);
  538. data->temp_crit_hyst = i2c_smbus_read_byte_data(client,
  539. TMP401_TEMP_CRIT_HYST);
  540. data->last_updated = jiffies;
  541. data->valid = 1;
  542. }
  543. mutex_unlock(&data->update_lock);
  544. return data;
  545. }
  546. static int __init tmp401_init(void)
  547. {
  548. return i2c_add_driver(&tmp401_driver);
  549. }
  550. static void __exit tmp401_exit(void)
  551. {
  552. i2c_del_driver(&tmp401_driver);
  553. }
  554. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  555. MODULE_DESCRIPTION("Texas Instruments TMP401 temperature sensor driver");
  556. MODULE_LICENSE("GPL");
  557. module_init(tmp401_init);
  558. module_exit(tmp401_exit);