max16065.c 22 KB

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