tmp401.c 22 KB

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