stts751.c 21 KB

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