tmp401.c 22 KB

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