stts751.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. /*
  2. * STTS751 sensor driver
  3. *
  4. * Copyright (C) 2016-2017 Istituto Italiano di Tecnologia - RBCS - EDL
  5. * Robotics, Brain and Cognitive Sciences department
  6. * Electronic Design Laboratory
  7. *
  8. * Written by Andrea Merello <andrea.merello@gmail.com>
  9. *
  10. * Based on LM95241 driver and LM90 driver
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. */
  22. #include <linux/bitops.h>
  23. #include <linux/err.h>
  24. #include <linux/hwmon.h>
  25. #include <linux/hwmon-sysfs.h>
  26. #include <linux/i2c.h>
  27. #include <linux/init.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/jiffies.h>
  30. #include <linux/module.h>
  31. #include <linux/mutex.h>
  32. #include <linux/property.h>
  33. #include <linux/slab.h>
  34. #include <linux/sysfs.h>
  35. #include <linux/util_macros.h>
  36. #define DEVNAME "stts751"
  37. static const unsigned short normal_i2c[] = {
  38. 0x48, 0x49, 0x38, 0x39, /* STTS751-0 */
  39. 0x4A, 0x4B, 0x3A, 0x3B, /* STTS751-1 */
  40. I2C_CLIENT_END };
  41. #define STTS751_REG_TEMP_H 0x00
  42. #define STTS751_REG_STATUS 0x01
  43. #define STTS751_STATUS_TRIPT BIT(0)
  44. #define STTS751_STATUS_TRIPL BIT(5)
  45. #define STTS751_STATUS_TRIPH BIT(6)
  46. #define STTS751_REG_TEMP_L 0x02
  47. #define STTS751_REG_CONF 0x03
  48. #define STTS751_CONF_RES_MASK 0x0C
  49. #define STTS751_CONF_RES_SHIFT 2
  50. #define STTS751_CONF_EVENT_DIS BIT(7)
  51. #define STTS751_CONF_STOP BIT(6)
  52. #define STTS751_REG_RATE 0x04
  53. #define STTS751_REG_HLIM_H 0x05
  54. #define STTS751_REG_HLIM_L 0x06
  55. #define STTS751_REG_LLIM_H 0x07
  56. #define STTS751_REG_LLIM_L 0x08
  57. #define STTS751_REG_TLIM 0x20
  58. #define STTS751_REG_HYST 0x21
  59. #define STTS751_REG_SMBUS_TO 0x22
  60. #define STTS751_REG_PROD_ID 0xFD
  61. #define STTS751_REG_MAN_ID 0xFE
  62. #define STTS751_REG_REV_ID 0xFF
  63. #define STTS751_0_PROD_ID 0x00
  64. #define STTS751_1_PROD_ID 0x01
  65. #define ST_MAN_ID 0x53
  66. /*
  67. * Possible update intervals are (in mS):
  68. * 16000, 8000, 4000, 2000, 1000, 500, 250, 125, 62.5, 31.25
  69. * However we are not going to complicate things too much and we stick to the
  70. * approx value in mS.
  71. */
  72. static const int stts751_intervals[] = {
  73. 16000, 8000, 4000, 2000, 1000, 500, 250, 125, 63, 31
  74. };
  75. static const struct i2c_device_id stts751_id[] = {
  76. { "stts751", 0 },
  77. { }
  78. };
  79. struct stts751_priv {
  80. struct device *dev;
  81. struct i2c_client *client;
  82. struct mutex access_lock;
  83. u8 interval;
  84. int res;
  85. int event_max, event_min;
  86. int therm;
  87. int hyst;
  88. bool smbus_timeout;
  89. int temp;
  90. unsigned long last_update, last_alert_update;
  91. u8 config;
  92. bool min_alert, max_alert, therm_trip;
  93. bool data_valid, alert_valid;
  94. bool notify_max, notify_min;
  95. };
  96. /*
  97. * These functions converts temperature from HW format to integer format and
  98. * vice-vers. They are (mostly) taken from lm90 driver. Unit is in mC.
  99. */
  100. static int stts751_to_deg(s16 hw_val)
  101. {
  102. return hw_val * 125 / 32;
  103. }
  104. static s32 stts751_to_hw(int val)
  105. {
  106. return DIV_ROUND_CLOSEST(val, 125) * 32;
  107. }
  108. static int stts751_adjust_resolution(struct stts751_priv *priv)
  109. {
  110. u8 res;
  111. switch (priv->interval) {
  112. case 9:
  113. /* 10 bits */
  114. res = 0;
  115. break;
  116. case 8:
  117. /* 11 bits */
  118. res = 1;
  119. break;
  120. default:
  121. /* 12 bits */
  122. res = 3;
  123. break;
  124. }
  125. if (priv->res == res)
  126. return 0;
  127. priv->config &= ~STTS751_CONF_RES_MASK;
  128. priv->config |= res << STTS751_CONF_RES_SHIFT;
  129. dev_dbg(&priv->client->dev, "setting res %d. config %x",
  130. res, priv->config);
  131. priv->res = res;
  132. return i2c_smbus_write_byte_data(priv->client,
  133. STTS751_REG_CONF, priv->config);
  134. }
  135. static int stts751_update_temp(struct stts751_priv *priv)
  136. {
  137. s32 integer1, integer2, frac;
  138. /*
  139. * There is a trick here, like in the lm90 driver. We have to read two
  140. * registers to get the sensor temperature, but we have to beware a
  141. * conversion could occur between the readings. We could use the
  142. * one-shot conversion register, but we don't want to do this (disables
  143. * hardware monitoring). So the solution used here is to read the high
  144. * byte once, then the low byte, then the high byte again. If the new
  145. * high byte matches the old one, then we have a valid reading. Else we
  146. * have to read the low byte again, and now we believe we have a correct
  147. * reading.
  148. */
  149. integer1 = i2c_smbus_read_byte_data(priv->client, STTS751_REG_TEMP_H);
  150. if (integer1 < 0) {
  151. dev_dbg(&priv->client->dev,
  152. "I2C read failed (temp H). ret: %x\n", integer1);
  153. return integer1;
  154. }
  155. frac = i2c_smbus_read_byte_data(priv->client, STTS751_REG_TEMP_L);
  156. if (frac < 0) {
  157. dev_dbg(&priv->client->dev,
  158. "I2C read failed (temp L). ret: %x\n", frac);
  159. return frac;
  160. }
  161. integer2 = i2c_smbus_read_byte_data(priv->client, STTS751_REG_TEMP_H);
  162. if (integer2 < 0) {
  163. dev_dbg(&priv->client->dev,
  164. "I2C 2nd read failed (temp H). ret: %x\n", integer2);
  165. return integer2;
  166. }
  167. if (integer1 != integer2) {
  168. frac = i2c_smbus_read_byte_data(priv->client,
  169. STTS751_REG_TEMP_L);
  170. if (frac < 0) {
  171. dev_dbg(&priv->client->dev,
  172. "I2C 2nd read failed (temp L). ret: %x\n",
  173. frac);
  174. return frac;
  175. }
  176. }
  177. priv->temp = stts751_to_deg((integer1 << 8) | frac);
  178. return 0;
  179. }
  180. static int stts751_set_temp_reg16(struct stts751_priv *priv, int temp,
  181. u8 hreg, u8 lreg)
  182. {
  183. s32 hwval;
  184. int ret;
  185. hwval = stts751_to_hw(temp);
  186. ret = i2c_smbus_write_byte_data(priv->client, hreg, hwval >> 8);
  187. if (ret)
  188. return ret;
  189. return i2c_smbus_write_byte_data(priv->client, lreg, hwval & 0xff);
  190. }
  191. static int stts751_set_temp_reg8(struct stts751_priv *priv, int temp, u8 reg)
  192. {
  193. s32 hwval;
  194. hwval = stts751_to_hw(temp);
  195. return i2c_smbus_write_byte_data(priv->client, reg, hwval >> 8);
  196. }
  197. static int stts751_read_reg16(struct stts751_priv *priv, int *temp,
  198. u8 hreg, u8 lreg)
  199. {
  200. int integer, frac;
  201. integer = i2c_smbus_read_byte_data(priv->client, hreg);
  202. if (integer < 0)
  203. return integer;
  204. frac = i2c_smbus_read_byte_data(priv->client, lreg);
  205. if (frac < 0)
  206. return frac;
  207. *temp = stts751_to_deg((integer << 8) | frac);
  208. return 0;
  209. }
  210. static int stts751_read_reg8(struct stts751_priv *priv, int *temp, u8 reg)
  211. {
  212. int integer;
  213. integer = i2c_smbus_read_byte_data(priv->client, reg);
  214. if (integer < 0)
  215. return integer;
  216. *temp = stts751_to_deg(integer << 8);
  217. return 0;
  218. }
  219. /*
  220. * Update alert flags without waiting for cache to expire. We detects alerts
  221. * immediately for the sake of the alert handler; we still need to deal with
  222. * caching to workaround the fact that alarm flags int the status register,
  223. * despite what the datasheet claims, gets always cleared on read.
  224. */
  225. static int stts751_update_alert(struct stts751_priv *priv)
  226. {
  227. int ret;
  228. bool conv_done;
  229. int cache_time = msecs_to_jiffies(stts751_intervals[priv->interval]);
  230. /*
  231. * Add another 10% because if we run faster than the HW conversion
  232. * rate we will end up in reporting incorrectly alarms.
  233. */
  234. cache_time += cache_time / 10;
  235. ret = i2c_smbus_read_byte_data(priv->client, STTS751_REG_STATUS);
  236. if (ret < 0)
  237. return ret;
  238. dev_dbg(&priv->client->dev, "status reg %x\n", ret);
  239. conv_done = ret & (STTS751_STATUS_TRIPH | STTS751_STATUS_TRIPL);
  240. /*
  241. * Reset the cache if the cache time expired, or if we are sure
  242. * we have valid data from a device conversion, or if we know
  243. * our cache has been never written.
  244. *
  245. * Note that when the cache has been never written the point is
  246. * to correctly initialize the timestamp, rather than clearing
  247. * the cache values.
  248. *
  249. * Note that updating the cache timestamp when we get an alarm flag
  250. * is required, otherwise we could incorrectly report alarms to be zero.
  251. */
  252. if (time_after(jiffies, priv->last_alert_update + cache_time) ||
  253. conv_done || !priv->alert_valid) {
  254. priv->max_alert = false;
  255. priv->min_alert = false;
  256. priv->alert_valid = true;
  257. priv->last_alert_update = jiffies;
  258. dev_dbg(&priv->client->dev, "invalidating alert cache\n");
  259. }
  260. priv->max_alert |= !!(ret & STTS751_STATUS_TRIPH);
  261. priv->min_alert |= !!(ret & STTS751_STATUS_TRIPL);
  262. priv->therm_trip = !!(ret & STTS751_STATUS_TRIPT);
  263. dev_dbg(&priv->client->dev, "max_alert: %d, min_alert: %d, therm_trip: %d\n",
  264. priv->max_alert, priv->min_alert, priv->therm_trip);
  265. return 0;
  266. }
  267. static void stts751_alert(struct i2c_client *client,
  268. enum i2c_alert_protocol type, unsigned int data)
  269. {
  270. int ret;
  271. struct stts751_priv *priv = i2c_get_clientdata(client);
  272. if (type != I2C_PROTOCOL_SMBUS_ALERT)
  273. return;
  274. dev_dbg(&client->dev, "alert!");
  275. mutex_lock(&priv->access_lock);
  276. ret = stts751_update_alert(priv);
  277. if (ret < 0) {
  278. /* default to worst case */
  279. priv->max_alert = true;
  280. priv->min_alert = true;
  281. dev_warn(priv->dev,
  282. "Alert received, but can't communicate to the device. Triggering all alarms!");
  283. }
  284. if (priv->max_alert) {
  285. if (priv->notify_max)
  286. dev_notice(priv->dev, "got alert for HIGH temperature");
  287. priv->notify_max = false;
  288. /* unblock alert poll */
  289. sysfs_notify(&priv->dev->kobj, NULL, "temp1_max_alarm");
  290. }
  291. if (priv->min_alert) {
  292. if (priv->notify_min)
  293. dev_notice(priv->dev, "got alert for LOW temperature");
  294. priv->notify_min = false;
  295. /* unblock alert poll */
  296. sysfs_notify(&priv->dev->kobj, NULL, "temp1_min_alarm");
  297. }
  298. if (priv->min_alert || priv->max_alert)
  299. kobject_uevent(&priv->dev->kobj, KOBJ_CHANGE);
  300. mutex_unlock(&priv->access_lock);
  301. }
  302. static int stts751_update(struct stts751_priv *priv)
  303. {
  304. int ret;
  305. int cache_time = msecs_to_jiffies(stts751_intervals[priv->interval]);
  306. if (time_after(jiffies, priv->last_update + cache_time) ||
  307. !priv->data_valid) {
  308. ret = stts751_update_temp(priv);
  309. if (ret)
  310. return ret;
  311. ret = stts751_update_alert(priv);
  312. if (ret)
  313. return ret;
  314. priv->data_valid = true;
  315. priv->last_update = jiffies;
  316. }
  317. return 0;
  318. }
  319. static ssize_t show_max_alarm(struct device *dev, struct device_attribute *attr,
  320. char *buf)
  321. {
  322. int ret;
  323. struct stts751_priv *priv = dev_get_drvdata(dev);
  324. mutex_lock(&priv->access_lock);
  325. ret = stts751_update(priv);
  326. if (!ret)
  327. priv->notify_max = true;
  328. mutex_unlock(&priv->access_lock);
  329. if (ret < 0)
  330. return ret;
  331. return snprintf(buf, PAGE_SIZE - 1, "%d\n", priv->max_alert);
  332. }
  333. static ssize_t show_min_alarm(struct device *dev, struct device_attribute *attr,
  334. char *buf)
  335. {
  336. int ret;
  337. struct stts751_priv *priv = dev_get_drvdata(dev);
  338. mutex_lock(&priv->access_lock);
  339. ret = stts751_update(priv);
  340. if (!ret)
  341. priv->notify_min = true;
  342. mutex_unlock(&priv->access_lock);
  343. if (ret < 0)
  344. return ret;
  345. return snprintf(buf, PAGE_SIZE - 1, "%d\n", priv->min_alert);
  346. }
  347. static ssize_t show_input(struct device *dev, struct device_attribute *attr,
  348. char *buf)
  349. {
  350. int ret;
  351. struct stts751_priv *priv = dev_get_drvdata(dev);
  352. mutex_lock(&priv->access_lock);
  353. ret = stts751_update(priv);
  354. mutex_unlock(&priv->access_lock);
  355. if (ret < 0)
  356. return ret;
  357. return snprintf(buf, PAGE_SIZE - 1, "%d\n", priv->temp);
  358. }
  359. static ssize_t show_therm(struct device *dev, struct device_attribute *attr,
  360. char *buf)
  361. {
  362. struct stts751_priv *priv = dev_get_drvdata(dev);
  363. return snprintf(buf, PAGE_SIZE - 1, "%d\n", priv->therm);
  364. }
  365. static ssize_t set_therm(struct device *dev, struct device_attribute *attr,
  366. const char *buf, size_t count)
  367. {
  368. int ret;
  369. long temp;
  370. struct stts751_priv *priv = dev_get_drvdata(dev);
  371. if (kstrtol(buf, 10, &temp) < 0)
  372. return -EINVAL;
  373. /* HW works in range -64C to +127.937C */
  374. temp = clamp_val(temp, -64000, 127937);
  375. mutex_lock(&priv->access_lock);
  376. ret = stts751_set_temp_reg8(priv, temp, STTS751_REG_TLIM);
  377. if (ret)
  378. goto exit;
  379. dev_dbg(&priv->client->dev, "setting therm %ld", temp);
  380. /*
  381. * hysteresis reg is relative to therm, so the HW does not need to be
  382. * adjusted, we need to update our local copy only.
  383. */
  384. priv->hyst = temp - (priv->therm - priv->hyst);
  385. priv->therm = temp;
  386. exit:
  387. mutex_unlock(&priv->access_lock);
  388. if (ret)
  389. return ret;
  390. return count;
  391. }
  392. static ssize_t show_hyst(struct device *dev, struct device_attribute *attr,
  393. char *buf)
  394. {
  395. struct stts751_priv *priv = dev_get_drvdata(dev);
  396. return snprintf(buf, PAGE_SIZE - 1, "%d\n", priv->hyst);
  397. }
  398. static ssize_t set_hyst(struct device *dev, struct device_attribute *attr,
  399. const char *buf, size_t count)
  400. {
  401. int ret;
  402. long temp;
  403. struct stts751_priv *priv = dev_get_drvdata(dev);
  404. if (kstrtol(buf, 10, &temp) < 0)
  405. return -EINVAL;
  406. mutex_lock(&priv->access_lock);
  407. /* HW works in range -64C to +127.937C */
  408. temp = clamp_val(temp, -64000, priv->therm);
  409. priv->hyst = temp;
  410. dev_dbg(&priv->client->dev, "setting hyst %ld", temp);
  411. temp = priv->therm - temp;
  412. ret = stts751_set_temp_reg8(priv, temp, STTS751_REG_HYST);
  413. mutex_unlock(&priv->access_lock);
  414. if (ret)
  415. return ret;
  416. return count;
  417. }
  418. static ssize_t show_therm_trip(struct device *dev,
  419. struct device_attribute *attr, char *buf)
  420. {
  421. int ret;
  422. struct stts751_priv *priv = dev_get_drvdata(dev);
  423. mutex_lock(&priv->access_lock);
  424. ret = stts751_update(priv);
  425. mutex_unlock(&priv->access_lock);
  426. if (ret < 0)
  427. return ret;
  428. return snprintf(buf, PAGE_SIZE - 1, "%d\n", priv->therm_trip);
  429. }
  430. static ssize_t show_max(struct device *dev, struct device_attribute *attr,
  431. char *buf)
  432. {
  433. struct stts751_priv *priv = dev_get_drvdata(dev);
  434. return snprintf(buf, PAGE_SIZE - 1, "%d\n", priv->event_max);
  435. }
  436. static ssize_t set_max(struct device *dev, struct device_attribute *attr,
  437. const char *buf, size_t count)
  438. {
  439. int ret;
  440. long temp;
  441. struct stts751_priv *priv = dev_get_drvdata(dev);
  442. if (kstrtol(buf, 10, &temp) < 0)
  443. return -EINVAL;
  444. mutex_lock(&priv->access_lock);
  445. /* HW works in range -64C to +127.937C */
  446. temp = clamp_val(temp, priv->event_min, 127937);
  447. ret = stts751_set_temp_reg16(priv, temp,
  448. STTS751_REG_HLIM_H, STTS751_REG_HLIM_L);
  449. if (ret)
  450. goto exit;
  451. dev_dbg(&priv->client->dev, "setting event max %ld", temp);
  452. priv->event_max = temp;
  453. ret = count;
  454. exit:
  455. mutex_unlock(&priv->access_lock);
  456. return ret;
  457. }
  458. static ssize_t show_min(struct device *dev, struct device_attribute *attr,
  459. char *buf)
  460. {
  461. struct stts751_priv *priv = dev_get_drvdata(dev);
  462. return snprintf(buf, PAGE_SIZE - 1, "%d\n", priv->event_min);
  463. }
  464. static ssize_t set_min(struct device *dev, struct device_attribute *attr,
  465. const char *buf, size_t count)
  466. {
  467. int ret;
  468. long temp;
  469. struct stts751_priv *priv = dev_get_drvdata(dev);
  470. if (kstrtol(buf, 10, &temp) < 0)
  471. return -EINVAL;
  472. mutex_lock(&priv->access_lock);
  473. /* HW works in range -64C to +127.937C */
  474. temp = clamp_val(temp, -64000, priv->event_max);
  475. ret = stts751_set_temp_reg16(priv, temp,
  476. STTS751_REG_LLIM_H, STTS751_REG_LLIM_L);
  477. if (ret)
  478. goto exit;
  479. dev_dbg(&priv->client->dev, "setting event min %ld", temp);
  480. priv->event_min = temp;
  481. ret = count;
  482. exit:
  483. mutex_unlock(&priv->access_lock);
  484. return ret;
  485. }
  486. static ssize_t show_interval(struct device *dev, struct device_attribute *attr,
  487. char *buf)
  488. {
  489. struct stts751_priv *priv = dev_get_drvdata(dev);
  490. return snprintf(buf, PAGE_SIZE - 1, "%d\n",
  491. stts751_intervals[priv->interval]);
  492. }
  493. static ssize_t set_interval(struct device *dev, struct device_attribute *attr,
  494. const char *buf, size_t count)
  495. {
  496. unsigned long val;
  497. int idx;
  498. int ret = count;
  499. struct stts751_priv *priv = dev_get_drvdata(dev);
  500. if (kstrtoul(buf, 10, &val) < 0)
  501. return -EINVAL;
  502. idx = find_closest_descending(val, stts751_intervals,
  503. ARRAY_SIZE(stts751_intervals));
  504. dev_dbg(&priv->client->dev, "setting interval. req:%lu, idx: %d, val: %d",
  505. val, idx, stts751_intervals[idx]);
  506. mutex_lock(&priv->access_lock);
  507. if (priv->interval == idx)
  508. goto exit;
  509. /*
  510. * In early development stages I've become suspicious about the chip
  511. * starting to misbehave if I ever set, even briefly, an invalid
  512. * configuration. While I'm not sure this is really needed, be
  513. * conservative and set rate/resolution in such an order that avoids
  514. * passing through an invalid configuration.
  515. */
  516. /* speed up: lower the resolution, then modify convrate */
  517. if (priv->interval < idx) {
  518. dev_dbg(&priv->client->dev, "lower resolution, then modify convrate");
  519. priv->interval = idx;
  520. ret = stts751_adjust_resolution(priv);
  521. if (ret)
  522. goto exit;
  523. }
  524. ret = i2c_smbus_write_byte_data(priv->client, STTS751_REG_RATE, idx);
  525. if (ret)
  526. goto exit;
  527. /* slow down: modify convrate, then raise resolution */
  528. if (priv->interval != idx) {
  529. dev_dbg(&priv->client->dev, "modify convrate, then raise resolution");
  530. priv->interval = idx;
  531. ret = stts751_adjust_resolution(priv);
  532. if (ret)
  533. goto exit;
  534. }
  535. ret = count;
  536. exit:
  537. mutex_unlock(&priv->access_lock);
  538. return ret;
  539. }
  540. static int stts751_detect(struct i2c_client *new_client,
  541. struct i2c_board_info *info)
  542. {
  543. struct i2c_adapter *adapter = new_client->adapter;
  544. const char *name;
  545. int tmp;
  546. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  547. return -ENODEV;
  548. tmp = i2c_smbus_read_byte_data(new_client, STTS751_REG_MAN_ID);
  549. if (tmp != ST_MAN_ID)
  550. return -ENODEV;
  551. /* lower temperaure registers always have bits 0-3 set to zero */
  552. tmp = i2c_smbus_read_byte_data(new_client, STTS751_REG_TEMP_L);
  553. if (tmp & 0xf)
  554. return -ENODEV;
  555. tmp = i2c_smbus_read_byte_data(new_client, STTS751_REG_HLIM_L);
  556. if (tmp & 0xf)
  557. return -ENODEV;
  558. tmp = i2c_smbus_read_byte_data(new_client, STTS751_REG_LLIM_L);
  559. if (tmp & 0xf)
  560. return -ENODEV;
  561. /* smbus timeout register always have bits 0-7 set to zero */
  562. tmp = i2c_smbus_read_byte_data(new_client, STTS751_REG_SMBUS_TO);
  563. if (tmp & 0x7f)
  564. return -ENODEV;
  565. tmp = i2c_smbus_read_byte_data(new_client, STTS751_REG_PROD_ID);
  566. switch (tmp) {
  567. case STTS751_0_PROD_ID:
  568. name = "STTS751-0";
  569. break;
  570. case STTS751_1_PROD_ID:
  571. name = "STTS751-1";
  572. break;
  573. default:
  574. return -ENODEV;
  575. }
  576. dev_dbg(&new_client->dev, "Chip %s detected", name);
  577. strlcpy(info->type, stts751_id[0].name, I2C_NAME_SIZE);
  578. return 0;
  579. }
  580. static int stts751_read_chip_config(struct stts751_priv *priv)
  581. {
  582. int ret;
  583. int tmp;
  584. ret = i2c_smbus_read_byte_data(priv->client, STTS751_REG_CONF);
  585. if (ret < 0)
  586. return ret;
  587. priv->config = ret;
  588. priv->res = (ret & STTS751_CONF_RES_MASK) >> STTS751_CONF_RES_SHIFT;
  589. ret = i2c_smbus_read_byte_data(priv->client, STTS751_REG_RATE);
  590. if (ret < 0)
  591. return ret;
  592. priv->interval = ret;
  593. ret = stts751_read_reg16(priv, &priv->event_max,
  594. STTS751_REG_HLIM_H, STTS751_REG_HLIM_L);
  595. if (ret)
  596. return ret;
  597. ret = stts751_read_reg16(priv, &priv->event_min,
  598. STTS751_REG_LLIM_H, STTS751_REG_LLIM_L);
  599. if (ret)
  600. return ret;
  601. ret = stts751_read_reg8(priv, &priv->therm, STTS751_REG_TLIM);
  602. if (ret)
  603. return ret;
  604. ret = stts751_read_reg8(priv, &tmp, STTS751_REG_HYST);
  605. if (ret)
  606. return ret;
  607. priv->hyst = priv->therm - tmp;
  608. return 0;
  609. }
  610. static SENSOR_DEVICE_ATTR(temp1_input, 0444, show_input, NULL, 0);
  611. static SENSOR_DEVICE_ATTR(temp1_min, 0644, show_min, set_min, 0);
  612. static SENSOR_DEVICE_ATTR(temp1_max, 0644, show_max, set_max, 0);
  613. static SENSOR_DEVICE_ATTR(temp1_min_alarm, 0444, show_min_alarm, NULL, 0);
  614. static SENSOR_DEVICE_ATTR(temp1_max_alarm, 0444, show_max_alarm, NULL, 0);
  615. static SENSOR_DEVICE_ATTR(temp1_crit, 0644, show_therm, set_therm, 0);
  616. static SENSOR_DEVICE_ATTR(temp1_crit_hyst, 0644, show_hyst, set_hyst, 0);
  617. static SENSOR_DEVICE_ATTR(temp1_crit_alarm, 0444, show_therm_trip, NULL, 0);
  618. static SENSOR_DEVICE_ATTR(update_interval, 0644,
  619. show_interval, set_interval, 0);
  620. static struct attribute *stts751_attrs[] = {
  621. &sensor_dev_attr_temp1_input.dev_attr.attr,
  622. &sensor_dev_attr_temp1_min.dev_attr.attr,
  623. &sensor_dev_attr_temp1_max.dev_attr.attr,
  624. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  625. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  626. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  627. &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
  628. &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
  629. &sensor_dev_attr_update_interval.dev_attr.attr,
  630. NULL
  631. };
  632. ATTRIBUTE_GROUPS(stts751);
  633. static int stts751_probe(struct i2c_client *client,
  634. const struct i2c_device_id *id)
  635. {
  636. struct stts751_priv *priv;
  637. int ret;
  638. bool smbus_nto;
  639. int rev_id;
  640. priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
  641. if (!priv)
  642. return -ENOMEM;
  643. priv->client = client;
  644. priv->notify_max = true;
  645. priv->notify_min = true;
  646. i2c_set_clientdata(client, priv);
  647. mutex_init(&priv->access_lock);
  648. if (device_property_present(&client->dev,
  649. "smbus-timeout-disable")) {
  650. smbus_nto = device_property_read_bool(&client->dev,
  651. "smbus-timeout-disable");
  652. ret = i2c_smbus_write_byte_data(client, STTS751_REG_SMBUS_TO,
  653. smbus_nto ? 0 : 0x80);
  654. if (ret)
  655. return ret;
  656. }
  657. rev_id = i2c_smbus_read_byte_data(client, STTS751_REG_REV_ID);
  658. if (rev_id < 0)
  659. return -ENODEV;
  660. if (rev_id != 0x1) {
  661. dev_dbg(&client->dev, "Chip revision 0x%x is untested\n",
  662. rev_id);
  663. }
  664. ret = stts751_read_chip_config(priv);
  665. if (ret)
  666. return ret;
  667. priv->config &= ~(STTS751_CONF_STOP | STTS751_CONF_EVENT_DIS);
  668. ret = i2c_smbus_write_byte_data(client, STTS751_REG_CONF, priv->config);
  669. if (ret)
  670. return ret;
  671. priv->dev = devm_hwmon_device_register_with_groups(&client->dev,
  672. client->name, priv,
  673. stts751_groups);
  674. return PTR_ERR_OR_ZERO(priv->dev);
  675. }
  676. MODULE_DEVICE_TABLE(i2c, stts751_id);
  677. static struct i2c_driver stts751_driver = {
  678. .class = I2C_CLASS_HWMON,
  679. .driver = {
  680. .name = DEVNAME,
  681. },
  682. .probe = stts751_probe,
  683. .id_table = stts751_id,
  684. .detect = stts751_detect,
  685. .alert = stts751_alert,
  686. .address_list = normal_i2c,
  687. };
  688. module_i2c_driver(stts751_driver);
  689. MODULE_AUTHOR("Andrea Merello <andrea.merello@gmail.com>");
  690. MODULE_DESCRIPTION("STTS751 sensor driver");
  691. MODULE_LICENSE("GPL");