tmp401.c 19 KB

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