tmp401.c 18 KB

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