tmp401.c 18 KB

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