tmp401.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  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. * Cleanup and support for TMP431 and TMP432 by Guenter Roeck
  9. * Copyright (c) 2013 Guenter Roeck <linux@roeck-us.net>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. /*
  26. * Driver for the Texas Instruments TMP401 SMBUS temperature sensor IC.
  27. *
  28. * Note this IC is in some aspect similar to the LM90, but it has quite a
  29. * few differences too, for example the local temp has a higher resolution
  30. * and thus has 16 bits registers for its value and limit instead of 8 bits.
  31. */
  32. #include <linux/module.h>
  33. #include <linux/init.h>
  34. #include <linux/bitops.h>
  35. #include <linux/slab.h>
  36. #include <linux/jiffies.h>
  37. #include <linux/i2c.h>
  38. #include <linux/hwmon.h>
  39. #include <linux/hwmon-sysfs.h>
  40. #include <linux/err.h>
  41. #include <linux/mutex.h>
  42. #include <linux/sysfs.h>
  43. /* Addresses to scan */
  44. static const unsigned short normal_i2c[] = { 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
  45. enum chips { tmp401, tmp411, tmp431, tmp432 };
  46. /*
  47. * The TMP401 registers, note some registers have different addresses for
  48. * reading and writing
  49. */
  50. #define TMP401_STATUS 0x02
  51. #define TMP401_CONFIG_READ 0x03
  52. #define TMP401_CONFIG_WRITE 0x09
  53. #define TMP401_CONVERSION_RATE_READ 0x04
  54. #define TMP401_CONVERSION_RATE_WRITE 0x0A
  55. #define TMP401_TEMP_CRIT_HYST 0x21
  56. #define TMP401_MANUFACTURER_ID_REG 0xFE
  57. #define TMP401_DEVICE_ID_REG 0xFF
  58. static const u8 TMP401_TEMP_MSB_READ[6][2] = {
  59. { 0x00, 0x01 }, /* temp */
  60. { 0x06, 0x08 }, /* low limit */
  61. { 0x05, 0x07 }, /* high limit */
  62. { 0x20, 0x19 }, /* therm (crit) limit */
  63. { 0x30, 0x34 }, /* lowest */
  64. { 0x32, 0x36 }, /* highest */
  65. };
  66. static const u8 TMP401_TEMP_MSB_WRITE[6][2] = {
  67. { 0, 0 }, /* temp (unused) */
  68. { 0x0C, 0x0E }, /* low limit */
  69. { 0x0B, 0x0D }, /* high limit */
  70. { 0x20, 0x19 }, /* therm (crit) limit */
  71. { 0x30, 0x34 }, /* lowest */
  72. { 0x32, 0x36 }, /* highest */
  73. };
  74. static const u8 TMP401_TEMP_LSB[6][2] = {
  75. { 0x15, 0x10 }, /* temp */
  76. { 0x17, 0x14 }, /* low limit */
  77. { 0x16, 0x13 }, /* high limit */
  78. { 0, 0 }, /* therm (crit) limit (unused) */
  79. { 0x31, 0x35 }, /* lowest */
  80. { 0x33, 0x37 }, /* highest */
  81. };
  82. static const u8 TMP432_TEMP_MSB_READ[4][3] = {
  83. { 0x00, 0x01, 0x23 }, /* temp */
  84. { 0x06, 0x08, 0x16 }, /* low limit */
  85. { 0x05, 0x07, 0x15 }, /* high limit */
  86. { 0x20, 0x19, 0x1A }, /* therm (crit) limit */
  87. };
  88. static const u8 TMP432_TEMP_MSB_WRITE[4][3] = {
  89. { 0, 0, 0 }, /* temp - unused */
  90. { 0x0C, 0x0E, 0x16 }, /* low limit */
  91. { 0x0B, 0x0D, 0x15 }, /* high limit */
  92. { 0x20, 0x19, 0x1A }, /* therm (crit) limit */
  93. };
  94. static const u8 TMP432_TEMP_LSB[3][3] = {
  95. { 0x29, 0x10, 0x24 }, /* temp */
  96. { 0x3E, 0x14, 0x18 }, /* low limit */
  97. { 0x3D, 0x13, 0x17 }, /* high limit */
  98. };
  99. /* [0] = fault, [1] = low, [2] = high, [3] = therm/crit */
  100. static const u8 TMP432_STATUS_REG[] = {
  101. 0x1b, 0x36, 0x35, 0x37 };
  102. /* Flags */
  103. #define TMP401_CONFIG_RANGE BIT(2)
  104. #define TMP401_CONFIG_SHUTDOWN BIT(6)
  105. #define TMP401_STATUS_LOCAL_CRIT BIT(0)
  106. #define TMP401_STATUS_REMOTE_CRIT BIT(1)
  107. #define TMP401_STATUS_REMOTE_OPEN BIT(2)
  108. #define TMP401_STATUS_REMOTE_LOW BIT(3)
  109. #define TMP401_STATUS_REMOTE_HIGH BIT(4)
  110. #define TMP401_STATUS_LOCAL_LOW BIT(5)
  111. #define TMP401_STATUS_LOCAL_HIGH BIT(6)
  112. /* On TMP432, each status has its own register */
  113. #define TMP432_STATUS_LOCAL BIT(0)
  114. #define TMP432_STATUS_REMOTE1 BIT(1)
  115. #define TMP432_STATUS_REMOTE2 BIT(2)
  116. /* Manufacturer / Device ID's */
  117. #define TMP401_MANUFACTURER_ID 0x55
  118. #define TMP401_DEVICE_ID 0x11
  119. #define TMP411A_DEVICE_ID 0x12
  120. #define TMP411B_DEVICE_ID 0x13
  121. #define TMP411C_DEVICE_ID 0x10
  122. #define TMP431_DEVICE_ID 0x31
  123. #define TMP432_DEVICE_ID 0x32
  124. /*
  125. * Driver data (common to all clients)
  126. */
  127. static const struct i2c_device_id tmp401_id[] = {
  128. { "tmp401", tmp401 },
  129. { "tmp411", tmp411 },
  130. { "tmp431", tmp431 },
  131. { "tmp432", tmp432 },
  132. { }
  133. };
  134. MODULE_DEVICE_TABLE(i2c, tmp401_id);
  135. /*
  136. * Client data (each client gets its own)
  137. */
  138. struct tmp401_data {
  139. struct i2c_client *client;
  140. const struct attribute_group *groups[3];
  141. struct mutex update_lock;
  142. char valid; /* zero until following fields are valid */
  143. unsigned long last_updated; /* in jiffies */
  144. enum chips kind;
  145. unsigned int update_interval; /* in milliseconds */
  146. /* register values */
  147. u8 status[4];
  148. u8 config;
  149. u16 temp[6][3];
  150. u8 temp_crit_hyst;
  151. };
  152. /*
  153. * Sysfs attr show / store functions
  154. */
  155. static int tmp401_register_to_temp(u16 reg, u8 config)
  156. {
  157. int temp = reg;
  158. if (config & TMP401_CONFIG_RANGE)
  159. temp -= 64 * 256;
  160. return DIV_ROUND_CLOSEST(temp * 125, 32);
  161. }
  162. static u16 tmp401_temp_to_register(long temp, u8 config, int zbits)
  163. {
  164. if (config & TMP401_CONFIG_RANGE) {
  165. temp = clamp_val(temp, -64000, 191000);
  166. temp += 64000;
  167. } else
  168. temp = clamp_val(temp, 0, 127000);
  169. return DIV_ROUND_CLOSEST(temp * (1 << (8 - zbits)), 1000) << zbits;
  170. }
  171. static int tmp401_update_device_reg16(struct i2c_client *client,
  172. struct tmp401_data *data)
  173. {
  174. int i, j, val;
  175. int num_regs = data->kind == tmp411 ? 6 : 4;
  176. int num_sensors = data->kind == tmp432 ? 3 : 2;
  177. for (i = 0; i < num_sensors; i++) { /* local / r1 / r2 */
  178. for (j = 0; j < num_regs; j++) { /* temp / low / ... */
  179. u8 regaddr;
  180. /*
  181. * High byte must be read first immediately followed
  182. * by the low byte
  183. */
  184. regaddr = data->kind == tmp432 ?
  185. TMP432_TEMP_MSB_READ[j][i] :
  186. TMP401_TEMP_MSB_READ[j][i];
  187. val = i2c_smbus_read_byte_data(client, regaddr);
  188. if (val < 0)
  189. return val;
  190. data->temp[j][i] = val << 8;
  191. if (j == 3) /* crit is msb only */
  192. continue;
  193. regaddr = data->kind == tmp432 ? TMP432_TEMP_LSB[j][i]
  194. : TMP401_TEMP_LSB[j][i];
  195. val = i2c_smbus_read_byte_data(client, regaddr);
  196. if (val < 0)
  197. return val;
  198. data->temp[j][i] |= val;
  199. }
  200. }
  201. return 0;
  202. }
  203. static struct tmp401_data *tmp401_update_device(struct device *dev)
  204. {
  205. struct tmp401_data *data = dev_get_drvdata(dev);
  206. struct i2c_client *client = data->client;
  207. struct tmp401_data *ret = data;
  208. int i, val;
  209. unsigned long next_update;
  210. mutex_lock(&data->update_lock);
  211. next_update = data->last_updated +
  212. msecs_to_jiffies(data->update_interval);
  213. if (time_after(jiffies, next_update) || !data->valid) {
  214. if (data->kind != tmp432) {
  215. /*
  216. * The driver uses the TMP432 status format internally.
  217. * Convert status to TMP432 format for other chips.
  218. */
  219. val = i2c_smbus_read_byte_data(client, TMP401_STATUS);
  220. if (val < 0) {
  221. ret = ERR_PTR(val);
  222. goto abort;
  223. }
  224. data->status[0] =
  225. (val & TMP401_STATUS_REMOTE_OPEN) >> 1;
  226. data->status[1] =
  227. ((val & TMP401_STATUS_REMOTE_LOW) >> 2) |
  228. ((val & TMP401_STATUS_LOCAL_LOW) >> 5);
  229. data->status[2] =
  230. ((val & TMP401_STATUS_REMOTE_HIGH) >> 3) |
  231. ((val & TMP401_STATUS_LOCAL_HIGH) >> 6);
  232. data->status[3] = val & (TMP401_STATUS_LOCAL_CRIT
  233. | TMP401_STATUS_REMOTE_CRIT);
  234. } else {
  235. for (i = 0; i < ARRAY_SIZE(data->status); i++) {
  236. val = i2c_smbus_read_byte_data(client,
  237. TMP432_STATUS_REG[i]);
  238. if (val < 0) {
  239. ret = ERR_PTR(val);
  240. goto abort;
  241. }
  242. data->status[i] = val;
  243. }
  244. }
  245. val = i2c_smbus_read_byte_data(client, TMP401_CONFIG_READ);
  246. if (val < 0) {
  247. ret = ERR_PTR(val);
  248. goto abort;
  249. }
  250. data->config = val;
  251. val = tmp401_update_device_reg16(client, data);
  252. if (val < 0) {
  253. ret = ERR_PTR(val);
  254. goto abort;
  255. }
  256. val = i2c_smbus_read_byte_data(client, TMP401_TEMP_CRIT_HYST);
  257. if (val < 0) {
  258. ret = ERR_PTR(val);
  259. goto abort;
  260. }
  261. data->temp_crit_hyst = val;
  262. data->last_updated = jiffies;
  263. data->valid = 1;
  264. }
  265. abort:
  266. mutex_unlock(&data->update_lock);
  267. return ret;
  268. }
  269. static ssize_t show_temp(struct device *dev,
  270. struct device_attribute *devattr, char *buf)
  271. {
  272. int nr = to_sensor_dev_attr_2(devattr)->nr;
  273. int index = to_sensor_dev_attr_2(devattr)->index;
  274. struct tmp401_data *data = tmp401_update_device(dev);
  275. if (IS_ERR(data))
  276. return PTR_ERR(data);
  277. return sprintf(buf, "%d\n",
  278. tmp401_register_to_temp(data->temp[nr][index], data->config));
  279. }
  280. static ssize_t show_temp_crit_hyst(struct device *dev,
  281. struct device_attribute *devattr, char *buf)
  282. {
  283. int temp, index = to_sensor_dev_attr(devattr)->index;
  284. struct tmp401_data *data = tmp401_update_device(dev);
  285. if (IS_ERR(data))
  286. return PTR_ERR(data);
  287. mutex_lock(&data->update_lock);
  288. temp = tmp401_register_to_temp(data->temp[3][index], data->config);
  289. temp -= data->temp_crit_hyst * 1000;
  290. mutex_unlock(&data->update_lock);
  291. return sprintf(buf, "%d\n", temp);
  292. }
  293. static ssize_t show_status(struct device *dev,
  294. struct device_attribute *devattr, char *buf)
  295. {
  296. int nr = to_sensor_dev_attr_2(devattr)->nr;
  297. int mask = to_sensor_dev_attr_2(devattr)->index;
  298. struct tmp401_data *data = tmp401_update_device(dev);
  299. if (IS_ERR(data))
  300. return PTR_ERR(data);
  301. return sprintf(buf, "%d\n", !!(data->status[nr] & mask));
  302. }
  303. static ssize_t store_temp(struct device *dev, struct device_attribute *devattr,
  304. const char *buf, size_t count)
  305. {
  306. int nr = to_sensor_dev_attr_2(devattr)->nr;
  307. int index = to_sensor_dev_attr_2(devattr)->index;
  308. struct tmp401_data *data = dev_get_drvdata(dev);
  309. struct i2c_client *client = data->client;
  310. long val;
  311. u16 reg;
  312. u8 regaddr;
  313. if (kstrtol(buf, 10, &val))
  314. return -EINVAL;
  315. reg = tmp401_temp_to_register(val, data->config, nr == 3 ? 8 : 4);
  316. mutex_lock(&data->update_lock);
  317. regaddr = data->kind == tmp432 ? TMP432_TEMP_MSB_WRITE[nr][index]
  318. : TMP401_TEMP_MSB_WRITE[nr][index];
  319. i2c_smbus_write_byte_data(client, regaddr, reg >> 8);
  320. if (nr != 3) {
  321. regaddr = data->kind == tmp432 ? TMP432_TEMP_LSB[nr][index]
  322. : TMP401_TEMP_LSB[nr][index];
  323. i2c_smbus_write_byte_data(client, regaddr, reg & 0xFF);
  324. }
  325. data->temp[nr][index] = reg;
  326. mutex_unlock(&data->update_lock);
  327. return count;
  328. }
  329. static ssize_t store_temp_crit_hyst(struct device *dev, struct device_attribute
  330. *devattr, const char *buf, size_t count)
  331. {
  332. int temp, index = to_sensor_dev_attr(devattr)->index;
  333. struct tmp401_data *data = tmp401_update_device(dev);
  334. long val;
  335. u8 reg;
  336. if (IS_ERR(data))
  337. return PTR_ERR(data);
  338. if (kstrtol(buf, 10, &val))
  339. return -EINVAL;
  340. if (data->config & TMP401_CONFIG_RANGE)
  341. val = clamp_val(val, -64000, 191000);
  342. else
  343. val = clamp_val(val, 0, 127000);
  344. mutex_lock(&data->update_lock);
  345. temp = tmp401_register_to_temp(data->temp[3][index], data->config);
  346. val = clamp_val(val, temp - 255000, temp);
  347. reg = ((temp - val) + 500) / 1000;
  348. i2c_smbus_write_byte_data(data->client, TMP401_TEMP_CRIT_HYST,
  349. 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. struct tmp401_data *data = dev_get_drvdata(dev);
  363. struct i2c_client *client = data->client;
  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. mutex_lock(&data->update_lock);
  374. i2c_smbus_write_byte_data(client, TMP401_TEMP_MSB_WRITE[5][0], val);
  375. data->valid = 0;
  376. mutex_unlock(&data->update_lock);
  377. return count;
  378. }
  379. static ssize_t show_update_interval(struct device *dev,
  380. struct device_attribute *attr, char *buf)
  381. {
  382. struct tmp401_data *data = dev_get_drvdata(dev);
  383. return sprintf(buf, "%u\n", data->update_interval);
  384. }
  385. static ssize_t set_update_interval(struct device *dev,
  386. struct device_attribute *attr,
  387. const char *buf, size_t count)
  388. {
  389. struct tmp401_data *data = dev_get_drvdata(dev);
  390. struct i2c_client *client = data->client;
  391. unsigned long val;
  392. int err, rate;
  393. err = kstrtoul(buf, 10, &val);
  394. if (err)
  395. return err;
  396. /*
  397. * For valid rates, interval can be calculated as
  398. * interval = (1 << (7 - rate)) * 125;
  399. * Rounded rate is therefore
  400. * rate = 7 - __fls(interval * 4 / (125 * 3));
  401. * Use clamp_val() to avoid overflows, and to ensure valid input
  402. * for __fls.
  403. */
  404. val = clamp_val(val, 125, 16000);
  405. rate = 7 - __fls(val * 4 / (125 * 3));
  406. mutex_lock(&data->update_lock);
  407. i2c_smbus_write_byte_data(client, TMP401_CONVERSION_RATE_WRITE, rate);
  408. data->update_interval = (1 << (7 - rate)) * 125;
  409. mutex_unlock(&data->update_lock);
  410. return count;
  411. }
  412. static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
  413. static SENSOR_DEVICE_ATTR_2(temp1_min, S_IWUSR | S_IRUGO, show_temp,
  414. store_temp, 1, 0);
  415. static SENSOR_DEVICE_ATTR_2(temp1_max, S_IWUSR | S_IRUGO, show_temp,
  416. store_temp, 2, 0);
  417. static SENSOR_DEVICE_ATTR_2(temp1_crit, S_IWUSR | S_IRUGO, show_temp,
  418. store_temp, 3, 0);
  419. static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO,
  420. show_temp_crit_hyst, store_temp_crit_hyst, 0);
  421. static SENSOR_DEVICE_ATTR_2(temp1_min_alarm, S_IRUGO, show_status, NULL,
  422. 1, TMP432_STATUS_LOCAL);
  423. static SENSOR_DEVICE_ATTR_2(temp1_max_alarm, S_IRUGO, show_status, NULL,
  424. 2, TMP432_STATUS_LOCAL);
  425. static SENSOR_DEVICE_ATTR_2(temp1_crit_alarm, S_IRUGO, show_status, NULL,
  426. 3, TMP432_STATUS_LOCAL);
  427. static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 0, 1);
  428. static SENSOR_DEVICE_ATTR_2(temp2_min, S_IWUSR | S_IRUGO, show_temp,
  429. store_temp, 1, 1);
  430. static SENSOR_DEVICE_ATTR_2(temp2_max, S_IWUSR | S_IRUGO, show_temp,
  431. store_temp, 2, 1);
  432. static SENSOR_DEVICE_ATTR_2(temp2_crit, S_IWUSR | S_IRUGO, show_temp,
  433. store_temp, 3, 1);
  434. static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_temp_crit_hyst,
  435. NULL, 1);
  436. static SENSOR_DEVICE_ATTR_2(temp2_fault, S_IRUGO, show_status, NULL,
  437. 0, TMP432_STATUS_REMOTE1);
  438. static SENSOR_DEVICE_ATTR_2(temp2_min_alarm, S_IRUGO, show_status, NULL,
  439. 1, TMP432_STATUS_REMOTE1);
  440. static SENSOR_DEVICE_ATTR_2(temp2_max_alarm, S_IRUGO, show_status, NULL,
  441. 2, TMP432_STATUS_REMOTE1);
  442. static SENSOR_DEVICE_ATTR_2(temp2_crit_alarm, S_IRUGO, show_status, NULL,
  443. 3, TMP432_STATUS_REMOTE1);
  444. static DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR, show_update_interval,
  445. set_update_interval);
  446. static struct attribute *tmp401_attributes[] = {
  447. &sensor_dev_attr_temp1_input.dev_attr.attr,
  448. &sensor_dev_attr_temp1_min.dev_attr.attr,
  449. &sensor_dev_attr_temp1_max.dev_attr.attr,
  450. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  451. &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
  452. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  453. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  454. &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
  455. &sensor_dev_attr_temp2_input.dev_attr.attr,
  456. &sensor_dev_attr_temp2_min.dev_attr.attr,
  457. &sensor_dev_attr_temp2_max.dev_attr.attr,
  458. &sensor_dev_attr_temp2_crit.dev_attr.attr,
  459. &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
  460. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  461. &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
  462. &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
  463. &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
  464. &dev_attr_update_interval.attr,
  465. NULL
  466. };
  467. static const struct attribute_group tmp401_group = {
  468. .attrs = tmp401_attributes,
  469. };
  470. /*
  471. * Additional features of the TMP411 chip.
  472. * The TMP411 stores the minimum and maximum
  473. * temperature measured since power-on, chip-reset, or
  474. * minimum and maximum register reset for both the local
  475. * and remote channels.
  476. */
  477. static SENSOR_DEVICE_ATTR_2(temp1_lowest, S_IRUGO, show_temp, NULL, 4, 0);
  478. static SENSOR_DEVICE_ATTR_2(temp1_highest, S_IRUGO, show_temp, NULL, 5, 0);
  479. static SENSOR_DEVICE_ATTR_2(temp2_lowest, S_IRUGO, show_temp, NULL, 4, 1);
  480. static SENSOR_DEVICE_ATTR_2(temp2_highest, S_IRUGO, show_temp, NULL, 5, 1);
  481. static SENSOR_DEVICE_ATTR(temp_reset_history, S_IWUSR, NULL, reset_temp_history,
  482. 0);
  483. static struct attribute *tmp411_attributes[] = {
  484. &sensor_dev_attr_temp1_highest.dev_attr.attr,
  485. &sensor_dev_attr_temp1_lowest.dev_attr.attr,
  486. &sensor_dev_attr_temp2_highest.dev_attr.attr,
  487. &sensor_dev_attr_temp2_lowest.dev_attr.attr,
  488. &sensor_dev_attr_temp_reset_history.dev_attr.attr,
  489. NULL
  490. };
  491. static const struct attribute_group tmp411_group = {
  492. .attrs = tmp411_attributes,
  493. };
  494. static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 0, 2);
  495. static SENSOR_DEVICE_ATTR_2(temp3_min, S_IWUSR | S_IRUGO, show_temp,
  496. store_temp, 1, 2);
  497. static SENSOR_DEVICE_ATTR_2(temp3_max, S_IWUSR | S_IRUGO, show_temp,
  498. store_temp, 2, 2);
  499. static SENSOR_DEVICE_ATTR_2(temp3_crit, S_IWUSR | S_IRUGO, show_temp,
  500. store_temp, 3, 2);
  501. static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, show_temp_crit_hyst,
  502. NULL, 2);
  503. static SENSOR_DEVICE_ATTR_2(temp3_fault, S_IRUGO, show_status, NULL,
  504. 0, TMP432_STATUS_REMOTE2);
  505. static SENSOR_DEVICE_ATTR_2(temp3_min_alarm, S_IRUGO, show_status, NULL,
  506. 1, TMP432_STATUS_REMOTE2);
  507. static SENSOR_DEVICE_ATTR_2(temp3_max_alarm, S_IRUGO, show_status, NULL,
  508. 2, TMP432_STATUS_REMOTE2);
  509. static SENSOR_DEVICE_ATTR_2(temp3_crit_alarm, S_IRUGO, show_status, NULL,
  510. 3, TMP432_STATUS_REMOTE2);
  511. static struct attribute *tmp432_attributes[] = {
  512. &sensor_dev_attr_temp3_input.dev_attr.attr,
  513. &sensor_dev_attr_temp3_min.dev_attr.attr,
  514. &sensor_dev_attr_temp3_max.dev_attr.attr,
  515. &sensor_dev_attr_temp3_crit.dev_attr.attr,
  516. &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
  517. &sensor_dev_attr_temp3_fault.dev_attr.attr,
  518. &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
  519. &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
  520. &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
  521. NULL
  522. };
  523. static const struct attribute_group tmp432_group = {
  524. .attrs = tmp432_attributes,
  525. };
  526. /*
  527. * Begin non sysfs callback code (aka Real code)
  528. */
  529. static void tmp401_init_client(struct tmp401_data *data,
  530. struct i2c_client *client)
  531. {
  532. int config, config_orig;
  533. /* Set the conversion rate to 2 Hz */
  534. i2c_smbus_write_byte_data(client, TMP401_CONVERSION_RATE_WRITE, 5);
  535. data->update_interval = 500;
  536. /* Start conversions (disable shutdown if necessary) */
  537. config = i2c_smbus_read_byte_data(client, TMP401_CONFIG_READ);
  538. if (config < 0) {
  539. dev_warn(&client->dev, "Initialization failed!\n");
  540. return;
  541. }
  542. config_orig = config;
  543. config &= ~TMP401_CONFIG_SHUTDOWN;
  544. if (config != config_orig)
  545. i2c_smbus_write_byte_data(client, TMP401_CONFIG_WRITE, config);
  546. }
  547. static int tmp401_detect(struct i2c_client *client,
  548. struct i2c_board_info *info)
  549. {
  550. enum chips kind;
  551. struct i2c_adapter *adapter = client->adapter;
  552. u8 reg;
  553. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  554. return -ENODEV;
  555. /* Detect and identify the chip */
  556. reg = i2c_smbus_read_byte_data(client, TMP401_MANUFACTURER_ID_REG);
  557. if (reg != TMP401_MANUFACTURER_ID)
  558. return -ENODEV;
  559. reg = i2c_smbus_read_byte_data(client, TMP401_DEVICE_ID_REG);
  560. switch (reg) {
  561. case TMP401_DEVICE_ID:
  562. if (client->addr != 0x4c)
  563. return -ENODEV;
  564. kind = tmp401;
  565. break;
  566. case TMP411A_DEVICE_ID:
  567. if (client->addr != 0x4c)
  568. return -ENODEV;
  569. kind = tmp411;
  570. break;
  571. case TMP411B_DEVICE_ID:
  572. if (client->addr != 0x4d)
  573. return -ENODEV;
  574. kind = tmp411;
  575. break;
  576. case TMP411C_DEVICE_ID:
  577. if (client->addr != 0x4e)
  578. return -ENODEV;
  579. kind = tmp411;
  580. break;
  581. case TMP431_DEVICE_ID:
  582. if (client->addr == 0x4e)
  583. return -ENODEV;
  584. kind = tmp431;
  585. break;
  586. case TMP432_DEVICE_ID:
  587. if (client->addr == 0x4e)
  588. return -ENODEV;
  589. kind = tmp432;
  590. break;
  591. default:
  592. return -ENODEV;
  593. }
  594. reg = i2c_smbus_read_byte_data(client, TMP401_CONFIG_READ);
  595. if (reg & 0x1b)
  596. return -ENODEV;
  597. reg = i2c_smbus_read_byte_data(client, TMP401_CONVERSION_RATE_READ);
  598. /* Datasheet says: 0x1-0x6 */
  599. if (reg > 15)
  600. return -ENODEV;
  601. strlcpy(info->type, tmp401_id[kind].name, I2C_NAME_SIZE);
  602. return 0;
  603. }
  604. static int tmp401_probe(struct i2c_client *client,
  605. const struct i2c_device_id *id)
  606. {
  607. const char *names[] = { "TMP401", "TMP411", "TMP431", "TMP432" };
  608. struct device *dev = &client->dev;
  609. struct device *hwmon_dev;
  610. struct tmp401_data *data;
  611. int groups = 0;
  612. data = devm_kzalloc(dev, sizeof(struct tmp401_data), GFP_KERNEL);
  613. if (!data)
  614. return -ENOMEM;
  615. data->client = client;
  616. mutex_init(&data->update_lock);
  617. data->kind = id->driver_data;
  618. /* Initialize the TMP401 chip */
  619. tmp401_init_client(data, client);
  620. /* Register sysfs hooks */
  621. data->groups[groups++] = &tmp401_group;
  622. /* Register additional tmp411 sysfs hooks */
  623. if (data->kind == tmp411)
  624. data->groups[groups++] = &tmp411_group;
  625. /* Register additional tmp432 sysfs hooks */
  626. if (data->kind == tmp432)
  627. data->groups[groups++] = &tmp432_group;
  628. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  629. data, data->groups);
  630. if (IS_ERR(hwmon_dev))
  631. return PTR_ERR(hwmon_dev);
  632. dev_info(dev, "Detected TI %s chip\n", names[data->kind]);
  633. return 0;
  634. }
  635. static struct i2c_driver tmp401_driver = {
  636. .class = I2C_CLASS_HWMON,
  637. .driver = {
  638. .name = "tmp401",
  639. },
  640. .probe = tmp401_probe,
  641. .id_table = tmp401_id,
  642. .detect = tmp401_detect,
  643. .address_list = normal_i2c,
  644. };
  645. module_i2c_driver(tmp401_driver);
  646. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  647. MODULE_DESCRIPTION("Texas Instruments TMP401 temperature sensor driver");
  648. MODULE_LICENSE("GPL");