max16065.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. /*
  2. * Driver for
  3. * Maxim MAX16065/MAX16066 12-Channel/8-Channel, Flash-Configurable
  4. * System Managers with Nonvolatile Fault Registers
  5. * Maxim MAX16067/MAX16068 6-Channel, Flash-Configurable System Managers
  6. * with Nonvolatile Fault Registers
  7. * Maxim MAX16070/MAX16071 12-Channel/8-Channel, Flash-Configurable System
  8. * Monitors with Nonvolatile Fault Registers
  9. *
  10. * Copyright (C) 2011 Ericsson AB.
  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; version 2 of the License.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/err.h>
  20. #include <linux/slab.h>
  21. #include <linux/i2c.h>
  22. #include <linux/hwmon.h>
  23. #include <linux/hwmon-sysfs.h>
  24. #include <linux/jiffies.h>
  25. enum chips { max16065, max16066, max16067, max16068, max16070, max16071 };
  26. /*
  27. * Registers
  28. */
  29. #define MAX16065_ADC(x) ((x) * 2)
  30. #define MAX16065_CURR_SENSE 0x18
  31. #define MAX16065_CSP_ADC 0x19
  32. #define MAX16065_FAULT(x) (0x1b + (x))
  33. #define MAX16065_SCALE(x) (0x43 + (x))
  34. #define MAX16065_CURR_CONTROL 0x47
  35. #define MAX16065_LIMIT(l, x) (0x48 + (l) + (x) * 3) /*
  36. * l: limit
  37. * 0: min/max
  38. * 1: crit
  39. * 2: lcrit
  40. * x: ADC index
  41. */
  42. #define MAX16065_SW_ENABLE 0x73
  43. #define MAX16065_WARNING_OV (1 << 3) /* Set if secondary threshold is OV
  44. warning */
  45. #define MAX16065_CURR_ENABLE (1 << 0)
  46. #define MAX16065_NUM_LIMIT 3
  47. #define MAX16065_NUM_ADC 12 /* maximum number of ADC channels */
  48. static const int max16065_num_adc[] = {
  49. [max16065] = 12,
  50. [max16066] = 8,
  51. [max16067] = 6,
  52. [max16068] = 6,
  53. [max16070] = 12,
  54. [max16071] = 8,
  55. };
  56. static const bool max16065_have_secondary[] = {
  57. [max16065] = true,
  58. [max16066] = true,
  59. [max16067] = false,
  60. [max16068] = false,
  61. [max16070] = true,
  62. [max16071] = true,
  63. };
  64. static const bool max16065_have_current[] = {
  65. [max16065] = true,
  66. [max16066] = true,
  67. [max16067] = false,
  68. [max16068] = false,
  69. [max16070] = true,
  70. [max16071] = true,
  71. };
  72. struct max16065_data {
  73. enum chips type;
  74. struct i2c_client *client;
  75. const struct attribute_group *groups[4];
  76. struct mutex update_lock;
  77. bool valid;
  78. unsigned long last_updated; /* in jiffies */
  79. int num_adc;
  80. bool have_current;
  81. int curr_gain;
  82. /* limits are in mV */
  83. int limit[MAX16065_NUM_LIMIT][MAX16065_NUM_ADC];
  84. int range[MAX16065_NUM_ADC + 1];/* voltage range */
  85. int adc[MAX16065_NUM_ADC + 1]; /* adc values (raw) including csp_adc */
  86. int curr_sense;
  87. int fault[2];
  88. };
  89. static const int max16065_adc_range[] = { 5560, 2780, 1390, 0 };
  90. static const int max16065_csp_adc_range[] = { 7000, 14000 };
  91. /* ADC registers have 10 bit resolution. */
  92. static inline int ADC_TO_MV(int adc, int range)
  93. {
  94. return (adc * range) / 1024;
  95. }
  96. /*
  97. * Limit registers have 8 bit resolution and match upper 8 bits of ADC
  98. * registers.
  99. */
  100. static inline int LIMIT_TO_MV(int limit, int range)
  101. {
  102. return limit * range / 256;
  103. }
  104. static inline int MV_TO_LIMIT(int mv, int range)
  105. {
  106. return clamp_val(DIV_ROUND_CLOSEST(mv * 256, range), 0, 255);
  107. }
  108. static inline int ADC_TO_CURR(int adc, int gain)
  109. {
  110. return adc * 1400000 / (gain * 255);
  111. }
  112. /*
  113. * max16065_read_adc()
  114. *
  115. * Read 16 bit value from <reg>, <reg+1>.
  116. * Upper 8 bits are in <reg>, lower 2 bits are in bits 7:6 of <reg+1>.
  117. */
  118. static int max16065_read_adc(struct i2c_client *client, int reg)
  119. {
  120. int rv;
  121. rv = i2c_smbus_read_word_swapped(client, reg);
  122. if (unlikely(rv < 0))
  123. return rv;
  124. return rv >> 6;
  125. }
  126. static struct max16065_data *max16065_update_device(struct device *dev)
  127. {
  128. struct max16065_data *data = dev_get_drvdata(dev);
  129. struct i2c_client *client = data->client;
  130. mutex_lock(&data->update_lock);
  131. if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
  132. int i;
  133. for (i = 0; i < data->num_adc; i++)
  134. data->adc[i]
  135. = max16065_read_adc(client, MAX16065_ADC(i));
  136. if (data->have_current) {
  137. data->adc[MAX16065_NUM_ADC]
  138. = max16065_read_adc(client, MAX16065_CSP_ADC);
  139. data->curr_sense
  140. = i2c_smbus_read_byte_data(client,
  141. MAX16065_CURR_SENSE);
  142. }
  143. for (i = 0; i < DIV_ROUND_UP(data->num_adc, 8); i++)
  144. data->fault[i]
  145. = i2c_smbus_read_byte_data(client, MAX16065_FAULT(i));
  146. data->last_updated = jiffies;
  147. data->valid = 1;
  148. }
  149. mutex_unlock(&data->update_lock);
  150. return data;
  151. }
  152. static ssize_t max16065_show_alarm(struct device *dev,
  153. struct device_attribute *da, char *buf)
  154. {
  155. struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(da);
  156. struct max16065_data *data = max16065_update_device(dev);
  157. int val = data->fault[attr2->nr];
  158. if (val < 0)
  159. return val;
  160. val &= (1 << attr2->index);
  161. if (val)
  162. i2c_smbus_write_byte_data(data->client,
  163. MAX16065_FAULT(attr2->nr), val);
  164. return snprintf(buf, PAGE_SIZE, "%d\n", !!val);
  165. }
  166. static ssize_t max16065_show_input(struct device *dev,
  167. struct device_attribute *da, char *buf)
  168. {
  169. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  170. struct max16065_data *data = max16065_update_device(dev);
  171. int adc = data->adc[attr->index];
  172. if (unlikely(adc < 0))
  173. return adc;
  174. return snprintf(buf, PAGE_SIZE, "%d\n",
  175. ADC_TO_MV(adc, data->range[attr->index]));
  176. }
  177. static ssize_t max16065_show_current(struct device *dev,
  178. struct device_attribute *da, char *buf)
  179. {
  180. struct max16065_data *data = max16065_update_device(dev);
  181. if (unlikely(data->curr_sense < 0))
  182. return data->curr_sense;
  183. return snprintf(buf, PAGE_SIZE, "%d\n",
  184. ADC_TO_CURR(data->curr_sense, data->curr_gain));
  185. }
  186. static ssize_t max16065_set_limit(struct device *dev,
  187. struct device_attribute *da,
  188. const char *buf, size_t count)
  189. {
  190. struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(da);
  191. struct max16065_data *data = dev_get_drvdata(dev);
  192. unsigned long val;
  193. int err;
  194. int limit;
  195. err = kstrtoul(buf, 10, &val);
  196. if (unlikely(err < 0))
  197. return err;
  198. limit = MV_TO_LIMIT(val, data->range[attr2->index]);
  199. mutex_lock(&data->update_lock);
  200. data->limit[attr2->nr][attr2->index]
  201. = LIMIT_TO_MV(limit, data->range[attr2->index]);
  202. i2c_smbus_write_byte_data(data->client,
  203. MAX16065_LIMIT(attr2->nr, attr2->index),
  204. limit);
  205. mutex_unlock(&data->update_lock);
  206. return count;
  207. }
  208. static ssize_t max16065_show_limit(struct device *dev,
  209. struct device_attribute *da, char *buf)
  210. {
  211. struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(da);
  212. struct max16065_data *data = dev_get_drvdata(dev);
  213. return snprintf(buf, PAGE_SIZE, "%d\n",
  214. data->limit[attr2->nr][attr2->index]);
  215. }
  216. /* Construct a sensor_device_attribute structure for each register */
  217. /* Input voltages */
  218. static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, max16065_show_input, NULL, 0);
  219. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, max16065_show_input, NULL, 1);
  220. static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, max16065_show_input, NULL, 2);
  221. static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, max16065_show_input, NULL, 3);
  222. static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, max16065_show_input, NULL, 4);
  223. static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, max16065_show_input, NULL, 5);
  224. static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, max16065_show_input, NULL, 6);
  225. static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, max16065_show_input, NULL, 7);
  226. static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, max16065_show_input, NULL, 8);
  227. static SENSOR_DEVICE_ATTR(in9_input, S_IRUGO, max16065_show_input, NULL, 9);
  228. static SENSOR_DEVICE_ATTR(in10_input, S_IRUGO, max16065_show_input, NULL, 10);
  229. static SENSOR_DEVICE_ATTR(in11_input, S_IRUGO, max16065_show_input, NULL, 11);
  230. static SENSOR_DEVICE_ATTR(in12_input, S_IRUGO, max16065_show_input, NULL, 12);
  231. /* Input voltages lcrit */
  232. static SENSOR_DEVICE_ATTR_2(in0_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit,
  233. max16065_set_limit, 2, 0);
  234. static SENSOR_DEVICE_ATTR_2(in1_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit,
  235. max16065_set_limit, 2, 1);
  236. static SENSOR_DEVICE_ATTR_2(in2_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit,
  237. max16065_set_limit, 2, 2);
  238. static SENSOR_DEVICE_ATTR_2(in3_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit,
  239. max16065_set_limit, 2, 3);
  240. static SENSOR_DEVICE_ATTR_2(in4_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit,
  241. max16065_set_limit, 2, 4);
  242. static SENSOR_DEVICE_ATTR_2(in5_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit,
  243. max16065_set_limit, 2, 5);
  244. static SENSOR_DEVICE_ATTR_2(in6_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit,
  245. max16065_set_limit, 2, 6);
  246. static SENSOR_DEVICE_ATTR_2(in7_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit,
  247. max16065_set_limit, 2, 7);
  248. static SENSOR_DEVICE_ATTR_2(in8_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit,
  249. max16065_set_limit, 2, 8);
  250. static SENSOR_DEVICE_ATTR_2(in9_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit,
  251. max16065_set_limit, 2, 9);
  252. static SENSOR_DEVICE_ATTR_2(in10_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit,
  253. max16065_set_limit, 2, 10);
  254. static SENSOR_DEVICE_ATTR_2(in11_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit,
  255. max16065_set_limit, 2, 11);
  256. /* Input voltages crit */
  257. static SENSOR_DEVICE_ATTR_2(in0_crit, S_IWUSR | S_IRUGO, max16065_show_limit,
  258. max16065_set_limit, 1, 0);
  259. static SENSOR_DEVICE_ATTR_2(in1_crit, S_IWUSR | S_IRUGO, max16065_show_limit,
  260. max16065_set_limit, 1, 1);
  261. static SENSOR_DEVICE_ATTR_2(in2_crit, S_IWUSR | S_IRUGO, max16065_show_limit,
  262. max16065_set_limit, 1, 2);
  263. static SENSOR_DEVICE_ATTR_2(in3_crit, S_IWUSR | S_IRUGO, max16065_show_limit,
  264. max16065_set_limit, 1, 3);
  265. static SENSOR_DEVICE_ATTR_2(in4_crit, S_IWUSR | S_IRUGO, max16065_show_limit,
  266. max16065_set_limit, 1, 4);
  267. static SENSOR_DEVICE_ATTR_2(in5_crit, S_IWUSR | S_IRUGO, max16065_show_limit,
  268. max16065_set_limit, 1, 5);
  269. static SENSOR_DEVICE_ATTR_2(in6_crit, S_IWUSR | S_IRUGO, max16065_show_limit,
  270. max16065_set_limit, 1, 6);
  271. static SENSOR_DEVICE_ATTR_2(in7_crit, S_IWUSR | S_IRUGO, max16065_show_limit,
  272. max16065_set_limit, 1, 7);
  273. static SENSOR_DEVICE_ATTR_2(in8_crit, S_IWUSR | S_IRUGO, max16065_show_limit,
  274. max16065_set_limit, 1, 8);
  275. static SENSOR_DEVICE_ATTR_2(in9_crit, S_IWUSR | S_IRUGO, max16065_show_limit,
  276. max16065_set_limit, 1, 9);
  277. static SENSOR_DEVICE_ATTR_2(in10_crit, S_IWUSR | S_IRUGO, max16065_show_limit,
  278. max16065_set_limit, 1, 10);
  279. static SENSOR_DEVICE_ATTR_2(in11_crit, S_IWUSR | S_IRUGO, max16065_show_limit,
  280. max16065_set_limit, 1, 11);
  281. /* Input voltages min */
  282. static SENSOR_DEVICE_ATTR_2(in0_min, S_IWUSR | S_IRUGO, max16065_show_limit,
  283. max16065_set_limit, 0, 0);
  284. static SENSOR_DEVICE_ATTR_2(in1_min, S_IWUSR | S_IRUGO, max16065_show_limit,
  285. max16065_set_limit, 0, 1);
  286. static SENSOR_DEVICE_ATTR_2(in2_min, S_IWUSR | S_IRUGO, max16065_show_limit,
  287. max16065_set_limit, 0, 2);
  288. static SENSOR_DEVICE_ATTR_2(in3_min, S_IWUSR | S_IRUGO, max16065_show_limit,
  289. max16065_set_limit, 0, 3);
  290. static SENSOR_DEVICE_ATTR_2(in4_min, S_IWUSR | S_IRUGO, max16065_show_limit,
  291. max16065_set_limit, 0, 4);
  292. static SENSOR_DEVICE_ATTR_2(in5_min, S_IWUSR | S_IRUGO, max16065_show_limit,
  293. max16065_set_limit, 0, 5);
  294. static SENSOR_DEVICE_ATTR_2(in6_min, S_IWUSR | S_IRUGO, max16065_show_limit,
  295. max16065_set_limit, 0, 6);
  296. static SENSOR_DEVICE_ATTR_2(in7_min, S_IWUSR | S_IRUGO, max16065_show_limit,
  297. max16065_set_limit, 0, 7);
  298. static SENSOR_DEVICE_ATTR_2(in8_min, S_IWUSR | S_IRUGO, max16065_show_limit,
  299. max16065_set_limit, 0, 8);
  300. static SENSOR_DEVICE_ATTR_2(in9_min, S_IWUSR | S_IRUGO, max16065_show_limit,
  301. max16065_set_limit, 0, 9);
  302. static SENSOR_DEVICE_ATTR_2(in10_min, S_IWUSR | S_IRUGO, max16065_show_limit,
  303. max16065_set_limit, 0, 10);
  304. static SENSOR_DEVICE_ATTR_2(in11_min, S_IWUSR | S_IRUGO, max16065_show_limit,
  305. max16065_set_limit, 0, 11);
  306. /* Input voltages max */
  307. static SENSOR_DEVICE_ATTR_2(in0_max, S_IWUSR | S_IRUGO, max16065_show_limit,
  308. max16065_set_limit, 0, 0);
  309. static SENSOR_DEVICE_ATTR_2(in1_max, S_IWUSR | S_IRUGO, max16065_show_limit,
  310. max16065_set_limit, 0, 1);
  311. static SENSOR_DEVICE_ATTR_2(in2_max, S_IWUSR | S_IRUGO, max16065_show_limit,
  312. max16065_set_limit, 0, 2);
  313. static SENSOR_DEVICE_ATTR_2(in3_max, S_IWUSR | S_IRUGO, max16065_show_limit,
  314. max16065_set_limit, 0, 3);
  315. static SENSOR_DEVICE_ATTR_2(in4_max, S_IWUSR | S_IRUGO, max16065_show_limit,
  316. max16065_set_limit, 0, 4);
  317. static SENSOR_DEVICE_ATTR_2(in5_max, S_IWUSR | S_IRUGO, max16065_show_limit,
  318. max16065_set_limit, 0, 5);
  319. static SENSOR_DEVICE_ATTR_2(in6_max, S_IWUSR | S_IRUGO, max16065_show_limit,
  320. max16065_set_limit, 0, 6);
  321. static SENSOR_DEVICE_ATTR_2(in7_max, S_IWUSR | S_IRUGO, max16065_show_limit,
  322. max16065_set_limit, 0, 7);
  323. static SENSOR_DEVICE_ATTR_2(in8_max, S_IWUSR | S_IRUGO, max16065_show_limit,
  324. max16065_set_limit, 0, 8);
  325. static SENSOR_DEVICE_ATTR_2(in9_max, S_IWUSR | S_IRUGO, max16065_show_limit,
  326. max16065_set_limit, 0, 9);
  327. static SENSOR_DEVICE_ATTR_2(in10_max, S_IWUSR | S_IRUGO, max16065_show_limit,
  328. max16065_set_limit, 0, 10);
  329. static SENSOR_DEVICE_ATTR_2(in11_max, S_IWUSR | S_IRUGO, max16065_show_limit,
  330. max16065_set_limit, 0, 11);
  331. /* alarms */
  332. static SENSOR_DEVICE_ATTR_2(in0_alarm, S_IRUGO, max16065_show_alarm, NULL,
  333. 0, 0);
  334. static SENSOR_DEVICE_ATTR_2(in1_alarm, S_IRUGO, max16065_show_alarm, NULL,
  335. 0, 1);
  336. static SENSOR_DEVICE_ATTR_2(in2_alarm, S_IRUGO, max16065_show_alarm, NULL,
  337. 0, 2);
  338. static SENSOR_DEVICE_ATTR_2(in3_alarm, S_IRUGO, max16065_show_alarm, NULL,
  339. 0, 3);
  340. static SENSOR_DEVICE_ATTR_2(in4_alarm, S_IRUGO, max16065_show_alarm, NULL,
  341. 0, 4);
  342. static SENSOR_DEVICE_ATTR_2(in5_alarm, S_IRUGO, max16065_show_alarm, NULL,
  343. 0, 5);
  344. static SENSOR_DEVICE_ATTR_2(in6_alarm, S_IRUGO, max16065_show_alarm, NULL,
  345. 0, 6);
  346. static SENSOR_DEVICE_ATTR_2(in7_alarm, S_IRUGO, max16065_show_alarm, NULL,
  347. 0, 7);
  348. static SENSOR_DEVICE_ATTR_2(in8_alarm, S_IRUGO, max16065_show_alarm, NULL,
  349. 1, 0);
  350. static SENSOR_DEVICE_ATTR_2(in9_alarm, S_IRUGO, max16065_show_alarm, NULL,
  351. 1, 1);
  352. static SENSOR_DEVICE_ATTR_2(in10_alarm, S_IRUGO, max16065_show_alarm, NULL,
  353. 1, 2);
  354. static SENSOR_DEVICE_ATTR_2(in11_alarm, S_IRUGO, max16065_show_alarm, NULL,
  355. 1, 3);
  356. /* Current and alarm */
  357. static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, max16065_show_current, NULL, 0);
  358. static SENSOR_DEVICE_ATTR_2(curr1_alarm, S_IRUGO, max16065_show_alarm, NULL,
  359. 1, 4);
  360. /*
  361. * Finally, construct an array of pointers to members of the above objects,
  362. * as required for sysfs_create_group()
  363. */
  364. static struct attribute *max16065_basic_attributes[] = {
  365. &sensor_dev_attr_in0_input.dev_attr.attr,
  366. &sensor_dev_attr_in0_lcrit.dev_attr.attr,
  367. &sensor_dev_attr_in0_crit.dev_attr.attr,
  368. &sensor_dev_attr_in0_alarm.dev_attr.attr,
  369. &sensor_dev_attr_in1_input.dev_attr.attr,
  370. &sensor_dev_attr_in1_lcrit.dev_attr.attr,
  371. &sensor_dev_attr_in1_crit.dev_attr.attr,
  372. &sensor_dev_attr_in1_alarm.dev_attr.attr,
  373. &sensor_dev_attr_in2_input.dev_attr.attr,
  374. &sensor_dev_attr_in2_lcrit.dev_attr.attr,
  375. &sensor_dev_attr_in2_crit.dev_attr.attr,
  376. &sensor_dev_attr_in2_alarm.dev_attr.attr,
  377. &sensor_dev_attr_in3_input.dev_attr.attr,
  378. &sensor_dev_attr_in3_lcrit.dev_attr.attr,
  379. &sensor_dev_attr_in3_crit.dev_attr.attr,
  380. &sensor_dev_attr_in3_alarm.dev_attr.attr,
  381. &sensor_dev_attr_in4_input.dev_attr.attr,
  382. &sensor_dev_attr_in4_lcrit.dev_attr.attr,
  383. &sensor_dev_attr_in4_crit.dev_attr.attr,
  384. &sensor_dev_attr_in4_alarm.dev_attr.attr,
  385. &sensor_dev_attr_in5_input.dev_attr.attr,
  386. &sensor_dev_attr_in5_lcrit.dev_attr.attr,
  387. &sensor_dev_attr_in5_crit.dev_attr.attr,
  388. &sensor_dev_attr_in5_alarm.dev_attr.attr,
  389. &sensor_dev_attr_in6_input.dev_attr.attr,
  390. &sensor_dev_attr_in6_lcrit.dev_attr.attr,
  391. &sensor_dev_attr_in6_crit.dev_attr.attr,
  392. &sensor_dev_attr_in6_alarm.dev_attr.attr,
  393. &sensor_dev_attr_in7_input.dev_attr.attr,
  394. &sensor_dev_attr_in7_lcrit.dev_attr.attr,
  395. &sensor_dev_attr_in7_crit.dev_attr.attr,
  396. &sensor_dev_attr_in7_alarm.dev_attr.attr,
  397. &sensor_dev_attr_in8_input.dev_attr.attr,
  398. &sensor_dev_attr_in8_lcrit.dev_attr.attr,
  399. &sensor_dev_attr_in8_crit.dev_attr.attr,
  400. &sensor_dev_attr_in8_alarm.dev_attr.attr,
  401. &sensor_dev_attr_in9_input.dev_attr.attr,
  402. &sensor_dev_attr_in9_lcrit.dev_attr.attr,
  403. &sensor_dev_attr_in9_crit.dev_attr.attr,
  404. &sensor_dev_attr_in9_alarm.dev_attr.attr,
  405. &sensor_dev_attr_in10_input.dev_attr.attr,
  406. &sensor_dev_attr_in10_lcrit.dev_attr.attr,
  407. &sensor_dev_attr_in10_crit.dev_attr.attr,
  408. &sensor_dev_attr_in10_alarm.dev_attr.attr,
  409. &sensor_dev_attr_in11_input.dev_attr.attr,
  410. &sensor_dev_attr_in11_lcrit.dev_attr.attr,
  411. &sensor_dev_attr_in11_crit.dev_attr.attr,
  412. &sensor_dev_attr_in11_alarm.dev_attr.attr,
  413. NULL
  414. };
  415. static struct attribute *max16065_current_attributes[] = {
  416. &sensor_dev_attr_in12_input.dev_attr.attr,
  417. &sensor_dev_attr_curr1_input.dev_attr.attr,
  418. &sensor_dev_attr_curr1_alarm.dev_attr.attr,
  419. NULL
  420. };
  421. static struct attribute *max16065_min_attributes[] = {
  422. &sensor_dev_attr_in0_min.dev_attr.attr,
  423. &sensor_dev_attr_in1_min.dev_attr.attr,
  424. &sensor_dev_attr_in2_min.dev_attr.attr,
  425. &sensor_dev_attr_in3_min.dev_attr.attr,
  426. &sensor_dev_attr_in4_min.dev_attr.attr,
  427. &sensor_dev_attr_in5_min.dev_attr.attr,
  428. &sensor_dev_attr_in6_min.dev_attr.attr,
  429. &sensor_dev_attr_in7_min.dev_attr.attr,
  430. &sensor_dev_attr_in8_min.dev_attr.attr,
  431. &sensor_dev_attr_in9_min.dev_attr.attr,
  432. &sensor_dev_attr_in10_min.dev_attr.attr,
  433. &sensor_dev_attr_in11_min.dev_attr.attr,
  434. NULL
  435. };
  436. static struct attribute *max16065_max_attributes[] = {
  437. &sensor_dev_attr_in0_max.dev_attr.attr,
  438. &sensor_dev_attr_in1_max.dev_attr.attr,
  439. &sensor_dev_attr_in2_max.dev_attr.attr,
  440. &sensor_dev_attr_in3_max.dev_attr.attr,
  441. &sensor_dev_attr_in4_max.dev_attr.attr,
  442. &sensor_dev_attr_in5_max.dev_attr.attr,
  443. &sensor_dev_attr_in6_max.dev_attr.attr,
  444. &sensor_dev_attr_in7_max.dev_attr.attr,
  445. &sensor_dev_attr_in8_max.dev_attr.attr,
  446. &sensor_dev_attr_in9_max.dev_attr.attr,
  447. &sensor_dev_attr_in10_max.dev_attr.attr,
  448. &sensor_dev_attr_in11_max.dev_attr.attr,
  449. NULL
  450. };
  451. static umode_t max16065_basic_is_visible(struct kobject *kobj,
  452. struct attribute *a, int n)
  453. {
  454. struct device *dev = container_of(kobj, struct device, kobj);
  455. struct max16065_data *data = dev_get_drvdata(dev);
  456. int index = n / 4;
  457. if (index >= data->num_adc || !data->range[index])
  458. return 0;
  459. return a->mode;
  460. }
  461. static umode_t max16065_secondary_is_visible(struct kobject *kobj,
  462. struct attribute *a, int index)
  463. {
  464. struct device *dev = container_of(kobj, struct device, kobj);
  465. struct max16065_data *data = dev_get_drvdata(dev);
  466. if (index >= data->num_adc)
  467. return 0;
  468. return a->mode;
  469. }
  470. static const struct attribute_group max16065_basic_group = {
  471. .attrs = max16065_basic_attributes,
  472. .is_visible = max16065_basic_is_visible,
  473. };
  474. static const struct attribute_group max16065_current_group = {
  475. .attrs = max16065_current_attributes,
  476. };
  477. static const struct attribute_group max16065_min_group = {
  478. .attrs = max16065_min_attributes,
  479. .is_visible = max16065_secondary_is_visible,
  480. };
  481. static const struct attribute_group max16065_max_group = {
  482. .attrs = max16065_max_attributes,
  483. .is_visible = max16065_secondary_is_visible,
  484. };
  485. static int max16065_probe(struct i2c_client *client,
  486. const struct i2c_device_id *id)
  487. {
  488. struct i2c_adapter *adapter = client->adapter;
  489. struct max16065_data *data;
  490. struct device *dev = &client->dev;
  491. struct device *hwmon_dev;
  492. int i, j, val;
  493. bool have_secondary; /* true if chip has secondary limits */
  494. bool secondary_is_max = false; /* secondary limits reflect max */
  495. int groups = 0;
  496. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
  497. | I2C_FUNC_SMBUS_READ_WORD_DATA))
  498. return -ENODEV;
  499. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  500. if (unlikely(!data))
  501. return -ENOMEM;
  502. data->client = client;
  503. mutex_init(&data->update_lock);
  504. data->num_adc = max16065_num_adc[id->driver_data];
  505. data->have_current = max16065_have_current[id->driver_data];
  506. have_secondary = max16065_have_secondary[id->driver_data];
  507. if (have_secondary) {
  508. val = i2c_smbus_read_byte_data(client, MAX16065_SW_ENABLE);
  509. if (unlikely(val < 0))
  510. return val;
  511. secondary_is_max = val & MAX16065_WARNING_OV;
  512. }
  513. /* Read scale registers, convert to range */
  514. for (i = 0; i < DIV_ROUND_UP(data->num_adc, 4); i++) {
  515. val = i2c_smbus_read_byte_data(client, MAX16065_SCALE(i));
  516. if (unlikely(val < 0))
  517. return val;
  518. for (j = 0; j < 4 && i * 4 + j < data->num_adc; j++) {
  519. data->range[i * 4 + j] =
  520. max16065_adc_range[(val >> (j * 2)) & 0x3];
  521. }
  522. }
  523. /* Read limits */
  524. for (i = 0; i < MAX16065_NUM_LIMIT; i++) {
  525. if (i == 0 && !have_secondary)
  526. continue;
  527. for (j = 0; j < data->num_adc; j++) {
  528. val = i2c_smbus_read_byte_data(client,
  529. MAX16065_LIMIT(i, j));
  530. if (unlikely(val < 0))
  531. return val;
  532. data->limit[i][j] = LIMIT_TO_MV(val, data->range[j]);
  533. }
  534. }
  535. /* sysfs hooks */
  536. data->groups[groups++] = &max16065_basic_group;
  537. if (have_secondary)
  538. data->groups[groups++] = secondary_is_max ?
  539. &max16065_max_group : &max16065_min_group;
  540. if (data->have_current) {
  541. val = i2c_smbus_read_byte_data(client, MAX16065_CURR_CONTROL);
  542. if (unlikely(val < 0))
  543. return val;
  544. if (val & MAX16065_CURR_ENABLE) {
  545. /*
  546. * Current gain is 6, 12, 24, 48 based on values in
  547. * bit 2,3.
  548. */
  549. data->curr_gain = 6 << ((val >> 2) & 0x03);
  550. data->range[MAX16065_NUM_ADC]
  551. = max16065_csp_adc_range[(val >> 1) & 0x01];
  552. data->groups[groups++] = &max16065_current_group;
  553. } else {
  554. data->have_current = false;
  555. }
  556. }
  557. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  558. data, data->groups);
  559. return PTR_ERR_OR_ZERO(hwmon_dev);
  560. }
  561. static const struct i2c_device_id max16065_id[] = {
  562. { "max16065", max16065 },
  563. { "max16066", max16066 },
  564. { "max16067", max16067 },
  565. { "max16068", max16068 },
  566. { "max16070", max16070 },
  567. { "max16071", max16071 },
  568. { }
  569. };
  570. MODULE_DEVICE_TABLE(i2c, max16065_id);
  571. /* This is the driver that will be inserted */
  572. static struct i2c_driver max16065_driver = {
  573. .driver = {
  574. .name = "max16065",
  575. },
  576. .probe = max16065_probe,
  577. .id_table = max16065_id,
  578. };
  579. module_i2c_driver(max16065_driver);
  580. MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
  581. MODULE_DESCRIPTION("MAX16065 driver");
  582. MODULE_LICENSE("GPL");