tmp401.c 19 KB

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