smm665.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. /*
  2. * Driver for SMM665 Power Controller / Monitor
  3. *
  4. * Copyright (C) 2010 Ericsson AB.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2 of the License.
  9. *
  10. * This driver should also work for SMM465, SMM764, and SMM766, but is untested
  11. * for those chips. Only monitoring functionality is implemented.
  12. *
  13. * Datasheets:
  14. * http://www.summitmicro.com/prod_select/summary/SMM665/SMM665B_2089_20.pdf
  15. * http://www.summitmicro.com/prod_select/summary/SMM766B/SMM766B_2122.pdf
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/err.h>
  21. #include <linux/slab.h>
  22. #include <linux/i2c.h>
  23. #include <linux/hwmon.h>
  24. #include <linux/hwmon-sysfs.h>
  25. #include <linux/delay.h>
  26. #include <linux/jiffies.h>
  27. /* Internal reference voltage (VREF, x 1000 */
  28. #define SMM665_VREF_ADC_X1000 1250
  29. /* module parameters */
  30. static int vref = SMM665_VREF_ADC_X1000;
  31. module_param(vref, int, 0);
  32. MODULE_PARM_DESC(vref, "Reference voltage in mV");
  33. enum chips { smm465, smm665, smm665c, smm764, smm766 };
  34. /*
  35. * ADC channel addresses
  36. */
  37. #define SMM665_MISC16_ADC_DATA_A 0x00
  38. #define SMM665_MISC16_ADC_DATA_B 0x01
  39. #define SMM665_MISC16_ADC_DATA_C 0x02
  40. #define SMM665_MISC16_ADC_DATA_D 0x03
  41. #define SMM665_MISC16_ADC_DATA_E 0x04
  42. #define SMM665_MISC16_ADC_DATA_F 0x05
  43. #define SMM665_MISC16_ADC_DATA_VDD 0x06
  44. #define SMM665_MISC16_ADC_DATA_12V 0x07
  45. #define SMM665_MISC16_ADC_DATA_INT_TEMP 0x08
  46. #define SMM665_MISC16_ADC_DATA_AIN1 0x09
  47. #define SMM665_MISC16_ADC_DATA_AIN2 0x0a
  48. /*
  49. * Command registers
  50. */
  51. #define SMM665_MISC8_CMD_STS 0x80
  52. #define SMM665_MISC8_STATUS1 0x81
  53. #define SMM665_MISC8_STATUSS2 0x82
  54. #define SMM665_MISC8_IO_POLARITY 0x83
  55. #define SMM665_MISC8_PUP_POLARITY 0x84
  56. #define SMM665_MISC8_ADOC_STATUS1 0x85
  57. #define SMM665_MISC8_ADOC_STATUS2 0x86
  58. #define SMM665_MISC8_WRITE_PROT 0x87
  59. #define SMM665_MISC8_STS_TRACK 0x88
  60. /*
  61. * Configuration registers and register groups
  62. */
  63. #define SMM665_ADOC_ENABLE 0x0d
  64. #define SMM665_LIMIT_BASE 0x80 /* First limit register */
  65. /*
  66. * Limit register bit masks
  67. */
  68. #define SMM665_TRIGGER_RST 0x8000
  69. #define SMM665_TRIGGER_HEALTHY 0x4000
  70. #define SMM665_TRIGGER_POWEROFF 0x2000
  71. #define SMM665_TRIGGER_SHUTDOWN 0x1000
  72. #define SMM665_ADC_MASK 0x03ff
  73. #define smm665_is_critical(lim) ((lim) & (SMM665_TRIGGER_RST \
  74. | SMM665_TRIGGER_POWEROFF \
  75. | SMM665_TRIGGER_SHUTDOWN))
  76. /*
  77. * Fault register bit definitions
  78. * Values are merged from status registers 1/2,
  79. * with status register 1 providing the upper 8 bits.
  80. */
  81. #define SMM665_FAULT_A 0x0001
  82. #define SMM665_FAULT_B 0x0002
  83. #define SMM665_FAULT_C 0x0004
  84. #define SMM665_FAULT_D 0x0008
  85. #define SMM665_FAULT_E 0x0010
  86. #define SMM665_FAULT_F 0x0020
  87. #define SMM665_FAULT_VDD 0x0040
  88. #define SMM665_FAULT_12V 0x0080
  89. #define SMM665_FAULT_TEMP 0x0100
  90. #define SMM665_FAULT_AIN1 0x0200
  91. #define SMM665_FAULT_AIN2 0x0400
  92. /*
  93. * I2C Register addresses
  94. *
  95. * The configuration register needs to be the configured base register.
  96. * The command/status register address is derived from it.
  97. */
  98. #define SMM665_REGMASK 0x78
  99. #define SMM665_CMDREG_BASE 0x48
  100. #define SMM665_CONFREG_BASE 0x50
  101. /*
  102. * Equations given by chip manufacturer to calculate voltage/temperature values
  103. * vref = Reference voltage on VREF_ADC pin (module parameter)
  104. * adc = 10bit ADC value read back from registers
  105. */
  106. /* Voltage A-F and VDD */
  107. #define SMM665_VMON_ADC_TO_VOLTS(adc) ((adc) * vref / 256)
  108. /* Voltage 12VIN */
  109. #define SMM665_12VIN_ADC_TO_VOLTS(adc) ((adc) * vref * 3 / 256)
  110. /* Voltage AIN1, AIN2 */
  111. #define SMM665_AIN_ADC_TO_VOLTS(adc) ((adc) * vref / 512)
  112. /* Temp Sensor */
  113. #define SMM665_TEMP_ADC_TO_CELSIUS(adc) (((adc) <= 511) ? \
  114. ((int)(adc) * 1000 / 4) : \
  115. (((int)(adc) - 0x400) * 1000 / 4))
  116. #define SMM665_NUM_ADC 11
  117. /*
  118. * Chip dependent ADC conversion time, in uS
  119. */
  120. #define SMM665_ADC_WAIT_SMM665 70
  121. #define SMM665_ADC_WAIT_SMM766 185
  122. struct smm665_data {
  123. enum chips type;
  124. int conversion_time; /* ADC conversion time */
  125. struct device *hwmon_dev;
  126. struct mutex update_lock;
  127. bool valid;
  128. unsigned long last_updated; /* in jiffies */
  129. u16 adc[SMM665_NUM_ADC]; /* adc values (raw) */
  130. u16 faults; /* fault status */
  131. /* The following values are in mV */
  132. int critical_min_limit[SMM665_NUM_ADC];
  133. int alarm_min_limit[SMM665_NUM_ADC];
  134. int critical_max_limit[SMM665_NUM_ADC];
  135. int alarm_max_limit[SMM665_NUM_ADC];
  136. struct i2c_client *cmdreg;
  137. };
  138. /*
  139. * smm665_read16()
  140. *
  141. * Read 16 bit value from <reg>, <reg+1>. Upper 8 bits are in <reg>.
  142. */
  143. static int smm665_read16(struct i2c_client *client, int reg)
  144. {
  145. int rv, val;
  146. rv = i2c_smbus_read_byte_data(client, reg);
  147. if (rv < 0)
  148. return rv;
  149. val = rv << 8;
  150. rv = i2c_smbus_read_byte_data(client, reg + 1);
  151. if (rv < 0)
  152. return rv;
  153. val |= rv;
  154. return val;
  155. }
  156. /*
  157. * Read adc value.
  158. */
  159. static int smm665_read_adc(struct smm665_data *data, int adc)
  160. {
  161. struct i2c_client *client = data->cmdreg;
  162. int rv;
  163. int radc;
  164. /*
  165. * Algorithm for reading ADC, per SMM665 datasheet
  166. *
  167. * {[S][addr][W][Ack]} {[offset][Ack]} {[S][addr][R][Nack]}
  168. * [wait conversion time]
  169. * {[S][addr][R][Ack]} {[datahi][Ack]} {[datalo][Ack][P]}
  170. *
  171. * To implement the first part of this exchange,
  172. * do a full read transaction and expect a failure/Nack.
  173. * This sets up the address pointer on the SMM665
  174. * and starts the ADC conversion.
  175. * Then do a two-byte read transaction.
  176. */
  177. rv = i2c_smbus_read_byte_data(client, adc << 3);
  178. if (rv != -ENXIO) {
  179. /*
  180. * We expect ENXIO to reflect NACK
  181. * (per Documentation/i2c/fault-codes).
  182. * Everything else is an error.
  183. */
  184. dev_dbg(&client->dev,
  185. "Unexpected return code %d when setting ADC index", rv);
  186. return (rv < 0) ? rv : -EIO;
  187. }
  188. udelay(data->conversion_time);
  189. /*
  190. * Now read two bytes.
  191. *
  192. * Neither i2c_smbus_read_byte() nor
  193. * i2c_smbus_read_block_data() worked here,
  194. * so use i2c_smbus_read_word_swapped() instead.
  195. * We could also try to use i2c_master_recv(),
  196. * but that is not always supported.
  197. */
  198. rv = i2c_smbus_read_word_swapped(client, 0);
  199. if (rv < 0) {
  200. dev_dbg(&client->dev, "Failed to read ADC value: error %d", rv);
  201. return -1;
  202. }
  203. /*
  204. * Validate/verify readback adc channel (in bit 11..14).
  205. */
  206. radc = (rv >> 11) & 0x0f;
  207. if (radc != adc) {
  208. dev_dbg(&client->dev, "Unexpected RADC: Expected %d got %d",
  209. adc, radc);
  210. return -EIO;
  211. }
  212. return rv & SMM665_ADC_MASK;
  213. }
  214. static struct smm665_data *smm665_update_device(struct device *dev)
  215. {
  216. struct i2c_client *client = to_i2c_client(dev);
  217. struct smm665_data *data = i2c_get_clientdata(client);
  218. struct smm665_data *ret = data;
  219. mutex_lock(&data->update_lock);
  220. if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
  221. int i, val;
  222. /*
  223. * read status registers
  224. */
  225. val = smm665_read16(client, SMM665_MISC8_STATUS1);
  226. if (unlikely(val < 0)) {
  227. ret = ERR_PTR(val);
  228. goto abort;
  229. }
  230. data->faults = val;
  231. /* Read adc registers */
  232. for (i = 0; i < SMM665_NUM_ADC; i++) {
  233. val = smm665_read_adc(data, i);
  234. if (unlikely(val < 0)) {
  235. ret = ERR_PTR(val);
  236. goto abort;
  237. }
  238. data->adc[i] = val;
  239. }
  240. data->last_updated = jiffies;
  241. data->valid = 1;
  242. }
  243. abort:
  244. mutex_unlock(&data->update_lock);
  245. return ret;
  246. }
  247. /* Return converted value from given adc */
  248. static int smm665_convert(u16 adcval, int index)
  249. {
  250. int val = 0;
  251. switch (index) {
  252. case SMM665_MISC16_ADC_DATA_12V:
  253. val = SMM665_12VIN_ADC_TO_VOLTS(adcval & SMM665_ADC_MASK);
  254. break;
  255. case SMM665_MISC16_ADC_DATA_VDD:
  256. case SMM665_MISC16_ADC_DATA_A:
  257. case SMM665_MISC16_ADC_DATA_B:
  258. case SMM665_MISC16_ADC_DATA_C:
  259. case SMM665_MISC16_ADC_DATA_D:
  260. case SMM665_MISC16_ADC_DATA_E:
  261. case SMM665_MISC16_ADC_DATA_F:
  262. val = SMM665_VMON_ADC_TO_VOLTS(adcval & SMM665_ADC_MASK);
  263. break;
  264. case SMM665_MISC16_ADC_DATA_AIN1:
  265. case SMM665_MISC16_ADC_DATA_AIN2:
  266. val = SMM665_AIN_ADC_TO_VOLTS(adcval & SMM665_ADC_MASK);
  267. break;
  268. case SMM665_MISC16_ADC_DATA_INT_TEMP:
  269. val = SMM665_TEMP_ADC_TO_CELSIUS(adcval & SMM665_ADC_MASK);
  270. break;
  271. default:
  272. /* If we get here, the developer messed up */
  273. WARN_ON_ONCE(1);
  274. break;
  275. }
  276. return val;
  277. }
  278. static int smm665_get_min(struct device *dev, int index)
  279. {
  280. struct i2c_client *client = to_i2c_client(dev);
  281. struct smm665_data *data = i2c_get_clientdata(client);
  282. return data->alarm_min_limit[index];
  283. }
  284. static int smm665_get_max(struct device *dev, int index)
  285. {
  286. struct i2c_client *client = to_i2c_client(dev);
  287. struct smm665_data *data = i2c_get_clientdata(client);
  288. return data->alarm_max_limit[index];
  289. }
  290. static int smm665_get_lcrit(struct device *dev, int index)
  291. {
  292. struct i2c_client *client = to_i2c_client(dev);
  293. struct smm665_data *data = i2c_get_clientdata(client);
  294. return data->critical_min_limit[index];
  295. }
  296. static int smm665_get_crit(struct device *dev, int index)
  297. {
  298. struct i2c_client *client = to_i2c_client(dev);
  299. struct smm665_data *data = i2c_get_clientdata(client);
  300. return data->critical_max_limit[index];
  301. }
  302. static ssize_t smm665_show_crit_alarm(struct device *dev,
  303. struct device_attribute *da, char *buf)
  304. {
  305. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  306. struct smm665_data *data = smm665_update_device(dev);
  307. int val = 0;
  308. if (IS_ERR(data))
  309. return PTR_ERR(data);
  310. if (data->faults & (1 << attr->index))
  311. val = 1;
  312. return snprintf(buf, PAGE_SIZE, "%d\n", val);
  313. }
  314. static ssize_t smm665_show_input(struct device *dev,
  315. struct device_attribute *da, char *buf)
  316. {
  317. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  318. struct smm665_data *data = smm665_update_device(dev);
  319. int adc = attr->index;
  320. int val;
  321. if (IS_ERR(data))
  322. return PTR_ERR(data);
  323. val = smm665_convert(data->adc[adc], adc);
  324. return snprintf(buf, PAGE_SIZE, "%d\n", val);
  325. }
  326. #define SMM665_SHOW(what) \
  327. static ssize_t smm665_show_##what(struct device *dev, \
  328. struct device_attribute *da, char *buf) \
  329. { \
  330. struct sensor_device_attribute *attr = to_sensor_dev_attr(da); \
  331. const int val = smm665_get_##what(dev, attr->index); \
  332. return snprintf(buf, PAGE_SIZE, "%d\n", val); \
  333. }
  334. SMM665_SHOW(min);
  335. SMM665_SHOW(max);
  336. SMM665_SHOW(lcrit);
  337. SMM665_SHOW(crit);
  338. /*
  339. * These macros are used below in constructing device attribute objects
  340. * for use with sysfs_create_group() to make a sysfs device file
  341. * for each register.
  342. */
  343. #define SMM665_ATTR(name, type, cmd_idx) \
  344. static SENSOR_DEVICE_ATTR(name##_##type, S_IRUGO, \
  345. smm665_show_##type, NULL, cmd_idx)
  346. /* Construct a sensor_device_attribute structure for each register */
  347. /* Input voltages */
  348. SMM665_ATTR(in1, input, SMM665_MISC16_ADC_DATA_12V);
  349. SMM665_ATTR(in2, input, SMM665_MISC16_ADC_DATA_VDD);
  350. SMM665_ATTR(in3, input, SMM665_MISC16_ADC_DATA_A);
  351. SMM665_ATTR(in4, input, SMM665_MISC16_ADC_DATA_B);
  352. SMM665_ATTR(in5, input, SMM665_MISC16_ADC_DATA_C);
  353. SMM665_ATTR(in6, input, SMM665_MISC16_ADC_DATA_D);
  354. SMM665_ATTR(in7, input, SMM665_MISC16_ADC_DATA_E);
  355. SMM665_ATTR(in8, input, SMM665_MISC16_ADC_DATA_F);
  356. SMM665_ATTR(in9, input, SMM665_MISC16_ADC_DATA_AIN1);
  357. SMM665_ATTR(in10, input, SMM665_MISC16_ADC_DATA_AIN2);
  358. /* Input voltages min */
  359. SMM665_ATTR(in1, min, SMM665_MISC16_ADC_DATA_12V);
  360. SMM665_ATTR(in2, min, SMM665_MISC16_ADC_DATA_VDD);
  361. SMM665_ATTR(in3, min, SMM665_MISC16_ADC_DATA_A);
  362. SMM665_ATTR(in4, min, SMM665_MISC16_ADC_DATA_B);
  363. SMM665_ATTR(in5, min, SMM665_MISC16_ADC_DATA_C);
  364. SMM665_ATTR(in6, min, SMM665_MISC16_ADC_DATA_D);
  365. SMM665_ATTR(in7, min, SMM665_MISC16_ADC_DATA_E);
  366. SMM665_ATTR(in8, min, SMM665_MISC16_ADC_DATA_F);
  367. SMM665_ATTR(in9, min, SMM665_MISC16_ADC_DATA_AIN1);
  368. SMM665_ATTR(in10, min, SMM665_MISC16_ADC_DATA_AIN2);
  369. /* Input voltages max */
  370. SMM665_ATTR(in1, max, SMM665_MISC16_ADC_DATA_12V);
  371. SMM665_ATTR(in2, max, SMM665_MISC16_ADC_DATA_VDD);
  372. SMM665_ATTR(in3, max, SMM665_MISC16_ADC_DATA_A);
  373. SMM665_ATTR(in4, max, SMM665_MISC16_ADC_DATA_B);
  374. SMM665_ATTR(in5, max, SMM665_MISC16_ADC_DATA_C);
  375. SMM665_ATTR(in6, max, SMM665_MISC16_ADC_DATA_D);
  376. SMM665_ATTR(in7, max, SMM665_MISC16_ADC_DATA_E);
  377. SMM665_ATTR(in8, max, SMM665_MISC16_ADC_DATA_F);
  378. SMM665_ATTR(in9, max, SMM665_MISC16_ADC_DATA_AIN1);
  379. SMM665_ATTR(in10, max, SMM665_MISC16_ADC_DATA_AIN2);
  380. /* Input voltages lcrit */
  381. SMM665_ATTR(in1, lcrit, SMM665_MISC16_ADC_DATA_12V);
  382. SMM665_ATTR(in2, lcrit, SMM665_MISC16_ADC_DATA_VDD);
  383. SMM665_ATTR(in3, lcrit, SMM665_MISC16_ADC_DATA_A);
  384. SMM665_ATTR(in4, lcrit, SMM665_MISC16_ADC_DATA_B);
  385. SMM665_ATTR(in5, lcrit, SMM665_MISC16_ADC_DATA_C);
  386. SMM665_ATTR(in6, lcrit, SMM665_MISC16_ADC_DATA_D);
  387. SMM665_ATTR(in7, lcrit, SMM665_MISC16_ADC_DATA_E);
  388. SMM665_ATTR(in8, lcrit, SMM665_MISC16_ADC_DATA_F);
  389. SMM665_ATTR(in9, lcrit, SMM665_MISC16_ADC_DATA_AIN1);
  390. SMM665_ATTR(in10, lcrit, SMM665_MISC16_ADC_DATA_AIN2);
  391. /* Input voltages crit */
  392. SMM665_ATTR(in1, crit, SMM665_MISC16_ADC_DATA_12V);
  393. SMM665_ATTR(in2, crit, SMM665_MISC16_ADC_DATA_VDD);
  394. SMM665_ATTR(in3, crit, SMM665_MISC16_ADC_DATA_A);
  395. SMM665_ATTR(in4, crit, SMM665_MISC16_ADC_DATA_B);
  396. SMM665_ATTR(in5, crit, SMM665_MISC16_ADC_DATA_C);
  397. SMM665_ATTR(in6, crit, SMM665_MISC16_ADC_DATA_D);
  398. SMM665_ATTR(in7, crit, SMM665_MISC16_ADC_DATA_E);
  399. SMM665_ATTR(in8, crit, SMM665_MISC16_ADC_DATA_F);
  400. SMM665_ATTR(in9, crit, SMM665_MISC16_ADC_DATA_AIN1);
  401. SMM665_ATTR(in10, crit, SMM665_MISC16_ADC_DATA_AIN2);
  402. /* critical alarms */
  403. SMM665_ATTR(in1, crit_alarm, SMM665_FAULT_12V);
  404. SMM665_ATTR(in2, crit_alarm, SMM665_FAULT_VDD);
  405. SMM665_ATTR(in3, crit_alarm, SMM665_FAULT_A);
  406. SMM665_ATTR(in4, crit_alarm, SMM665_FAULT_B);
  407. SMM665_ATTR(in5, crit_alarm, SMM665_FAULT_C);
  408. SMM665_ATTR(in6, crit_alarm, SMM665_FAULT_D);
  409. SMM665_ATTR(in7, crit_alarm, SMM665_FAULT_E);
  410. SMM665_ATTR(in8, crit_alarm, SMM665_FAULT_F);
  411. SMM665_ATTR(in9, crit_alarm, SMM665_FAULT_AIN1);
  412. SMM665_ATTR(in10, crit_alarm, SMM665_FAULT_AIN2);
  413. /* Temperature */
  414. SMM665_ATTR(temp1, input, SMM665_MISC16_ADC_DATA_INT_TEMP);
  415. SMM665_ATTR(temp1, min, SMM665_MISC16_ADC_DATA_INT_TEMP);
  416. SMM665_ATTR(temp1, max, SMM665_MISC16_ADC_DATA_INT_TEMP);
  417. SMM665_ATTR(temp1, lcrit, SMM665_MISC16_ADC_DATA_INT_TEMP);
  418. SMM665_ATTR(temp1, crit, SMM665_MISC16_ADC_DATA_INT_TEMP);
  419. SMM665_ATTR(temp1, crit_alarm, SMM665_FAULT_TEMP);
  420. /*
  421. * Finally, construct an array of pointers to members of the above objects,
  422. * as required for sysfs_create_group()
  423. */
  424. static struct attribute *smm665_attributes[] = {
  425. &sensor_dev_attr_in1_input.dev_attr.attr,
  426. &sensor_dev_attr_in1_min.dev_attr.attr,
  427. &sensor_dev_attr_in1_max.dev_attr.attr,
  428. &sensor_dev_attr_in1_lcrit.dev_attr.attr,
  429. &sensor_dev_attr_in1_crit.dev_attr.attr,
  430. &sensor_dev_attr_in1_crit_alarm.dev_attr.attr,
  431. &sensor_dev_attr_in2_input.dev_attr.attr,
  432. &sensor_dev_attr_in2_min.dev_attr.attr,
  433. &sensor_dev_attr_in2_max.dev_attr.attr,
  434. &sensor_dev_attr_in2_lcrit.dev_attr.attr,
  435. &sensor_dev_attr_in2_crit.dev_attr.attr,
  436. &sensor_dev_attr_in2_crit_alarm.dev_attr.attr,
  437. &sensor_dev_attr_in3_input.dev_attr.attr,
  438. &sensor_dev_attr_in3_min.dev_attr.attr,
  439. &sensor_dev_attr_in3_max.dev_attr.attr,
  440. &sensor_dev_attr_in3_lcrit.dev_attr.attr,
  441. &sensor_dev_attr_in3_crit.dev_attr.attr,
  442. &sensor_dev_attr_in3_crit_alarm.dev_attr.attr,
  443. &sensor_dev_attr_in4_input.dev_attr.attr,
  444. &sensor_dev_attr_in4_min.dev_attr.attr,
  445. &sensor_dev_attr_in4_max.dev_attr.attr,
  446. &sensor_dev_attr_in4_lcrit.dev_attr.attr,
  447. &sensor_dev_attr_in4_crit.dev_attr.attr,
  448. &sensor_dev_attr_in4_crit_alarm.dev_attr.attr,
  449. &sensor_dev_attr_in5_input.dev_attr.attr,
  450. &sensor_dev_attr_in5_min.dev_attr.attr,
  451. &sensor_dev_attr_in5_max.dev_attr.attr,
  452. &sensor_dev_attr_in5_lcrit.dev_attr.attr,
  453. &sensor_dev_attr_in5_crit.dev_attr.attr,
  454. &sensor_dev_attr_in5_crit_alarm.dev_attr.attr,
  455. &sensor_dev_attr_in6_input.dev_attr.attr,
  456. &sensor_dev_attr_in6_min.dev_attr.attr,
  457. &sensor_dev_attr_in6_max.dev_attr.attr,
  458. &sensor_dev_attr_in6_lcrit.dev_attr.attr,
  459. &sensor_dev_attr_in6_crit.dev_attr.attr,
  460. &sensor_dev_attr_in6_crit_alarm.dev_attr.attr,
  461. &sensor_dev_attr_in7_input.dev_attr.attr,
  462. &sensor_dev_attr_in7_min.dev_attr.attr,
  463. &sensor_dev_attr_in7_max.dev_attr.attr,
  464. &sensor_dev_attr_in7_lcrit.dev_attr.attr,
  465. &sensor_dev_attr_in7_crit.dev_attr.attr,
  466. &sensor_dev_attr_in7_crit_alarm.dev_attr.attr,
  467. &sensor_dev_attr_in8_input.dev_attr.attr,
  468. &sensor_dev_attr_in8_min.dev_attr.attr,
  469. &sensor_dev_attr_in8_max.dev_attr.attr,
  470. &sensor_dev_attr_in8_lcrit.dev_attr.attr,
  471. &sensor_dev_attr_in8_crit.dev_attr.attr,
  472. &sensor_dev_attr_in8_crit_alarm.dev_attr.attr,
  473. &sensor_dev_attr_in9_input.dev_attr.attr,
  474. &sensor_dev_attr_in9_min.dev_attr.attr,
  475. &sensor_dev_attr_in9_max.dev_attr.attr,
  476. &sensor_dev_attr_in9_lcrit.dev_attr.attr,
  477. &sensor_dev_attr_in9_crit.dev_attr.attr,
  478. &sensor_dev_attr_in9_crit_alarm.dev_attr.attr,
  479. &sensor_dev_attr_in10_input.dev_attr.attr,
  480. &sensor_dev_attr_in10_min.dev_attr.attr,
  481. &sensor_dev_attr_in10_max.dev_attr.attr,
  482. &sensor_dev_attr_in10_lcrit.dev_attr.attr,
  483. &sensor_dev_attr_in10_crit.dev_attr.attr,
  484. &sensor_dev_attr_in10_crit_alarm.dev_attr.attr,
  485. &sensor_dev_attr_temp1_input.dev_attr.attr,
  486. &sensor_dev_attr_temp1_min.dev_attr.attr,
  487. &sensor_dev_attr_temp1_max.dev_attr.attr,
  488. &sensor_dev_attr_temp1_lcrit.dev_attr.attr,
  489. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  490. &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
  491. NULL,
  492. };
  493. static const struct attribute_group smm665_group = {
  494. .attrs = smm665_attributes,
  495. };
  496. static int smm665_probe(struct i2c_client *client,
  497. const struct i2c_device_id *id)
  498. {
  499. struct i2c_adapter *adapter = client->adapter;
  500. struct smm665_data *data;
  501. int i, ret;
  502. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
  503. | I2C_FUNC_SMBUS_WORD_DATA))
  504. return -ENODEV;
  505. if (i2c_smbus_read_byte_data(client, SMM665_ADOC_ENABLE) < 0)
  506. return -ENODEV;
  507. data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
  508. if (!data)
  509. return -ENOMEM;
  510. i2c_set_clientdata(client, data);
  511. mutex_init(&data->update_lock);
  512. data->type = id->driver_data;
  513. data->cmdreg = i2c_new_dummy(adapter, (client->addr & ~SMM665_REGMASK)
  514. | SMM665_CMDREG_BASE);
  515. if (!data->cmdreg)
  516. return -ENOMEM;
  517. switch (data->type) {
  518. case smm465:
  519. case smm665:
  520. data->conversion_time = SMM665_ADC_WAIT_SMM665;
  521. break;
  522. case smm665c:
  523. case smm764:
  524. case smm766:
  525. data->conversion_time = SMM665_ADC_WAIT_SMM766;
  526. break;
  527. }
  528. ret = -ENODEV;
  529. if (i2c_smbus_read_byte_data(data->cmdreg, SMM665_MISC8_CMD_STS) < 0)
  530. goto out_unregister;
  531. /*
  532. * Read limits.
  533. *
  534. * Limit registers start with register SMM665_LIMIT_BASE.
  535. * Each channel uses 8 registers, providing four limit values
  536. * per channel. Each limit value requires two registers, with the
  537. * high byte in the first register and the low byte in the second
  538. * register. The first two limits are under limit values, followed
  539. * by two over limit values.
  540. *
  541. * Limit register order matches the ADC register order, so we use
  542. * ADC register defines throughout the code to index limit registers.
  543. *
  544. * We save the first retrieved value both as "critical" and "alarm"
  545. * value. The second value overwrites either the critical or the
  546. * alarm value, depending on its configuration. This ensures that both
  547. * critical and alarm values are initialized, even if both registers are
  548. * configured as critical or non-critical.
  549. */
  550. for (i = 0; i < SMM665_NUM_ADC; i++) {
  551. int val;
  552. val = smm665_read16(client, SMM665_LIMIT_BASE + i * 8);
  553. if (unlikely(val < 0))
  554. goto out_unregister;
  555. data->critical_min_limit[i] = data->alarm_min_limit[i]
  556. = smm665_convert(val, i);
  557. val = smm665_read16(client, SMM665_LIMIT_BASE + i * 8 + 2);
  558. if (unlikely(val < 0))
  559. goto out_unregister;
  560. if (smm665_is_critical(val))
  561. data->critical_min_limit[i] = smm665_convert(val, i);
  562. else
  563. data->alarm_min_limit[i] = smm665_convert(val, i);
  564. val = smm665_read16(client, SMM665_LIMIT_BASE + i * 8 + 4);
  565. if (unlikely(val < 0))
  566. goto out_unregister;
  567. data->critical_max_limit[i] = data->alarm_max_limit[i]
  568. = smm665_convert(val, i);
  569. val = smm665_read16(client, SMM665_LIMIT_BASE + i * 8 + 6);
  570. if (unlikely(val < 0))
  571. goto out_unregister;
  572. if (smm665_is_critical(val))
  573. data->critical_max_limit[i] = smm665_convert(val, i);
  574. else
  575. data->alarm_max_limit[i] = smm665_convert(val, i);
  576. }
  577. /* Register sysfs hooks */
  578. ret = sysfs_create_group(&client->dev.kobj, &smm665_group);
  579. if (ret)
  580. goto out_unregister;
  581. data->hwmon_dev = hwmon_device_register(&client->dev);
  582. if (IS_ERR(data->hwmon_dev)) {
  583. ret = PTR_ERR(data->hwmon_dev);
  584. goto out_remove_group;
  585. }
  586. return 0;
  587. out_remove_group:
  588. sysfs_remove_group(&client->dev.kobj, &smm665_group);
  589. out_unregister:
  590. i2c_unregister_device(data->cmdreg);
  591. return ret;
  592. }
  593. static int smm665_remove(struct i2c_client *client)
  594. {
  595. struct smm665_data *data = i2c_get_clientdata(client);
  596. i2c_unregister_device(data->cmdreg);
  597. hwmon_device_unregister(data->hwmon_dev);
  598. sysfs_remove_group(&client->dev.kobj, &smm665_group);
  599. return 0;
  600. }
  601. static const struct i2c_device_id smm665_id[] = {
  602. {"smm465", smm465},
  603. {"smm665", smm665},
  604. {"smm665c", smm665c},
  605. {"smm764", smm764},
  606. {"smm766", smm766},
  607. {}
  608. };
  609. MODULE_DEVICE_TABLE(i2c, smm665_id);
  610. /* This is the driver that will be inserted */
  611. static struct i2c_driver smm665_driver = {
  612. .driver = {
  613. .name = "smm665",
  614. },
  615. .probe = smm665_probe,
  616. .remove = smm665_remove,
  617. .id_table = smm665_id,
  618. };
  619. module_i2c_driver(smm665_driver);
  620. MODULE_AUTHOR("Guenter Roeck");
  621. MODULE_DESCRIPTION("SMM665 driver");
  622. MODULE_LICENSE("GPL");