tmp401.c 19 KB

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