ltc4245.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /*
  2. * Driver for Linear Technology LTC4245 I2C Multiple Supply Hot Swap Controller
  3. *
  4. * Copyright (C) 2008 Ira W. Snyder <iws@ovro.caltech.edu>
  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 is based on the ds1621 and ina209 drivers.
  11. *
  12. * Datasheet:
  13. * http://www.linear.com/pc/downloadDocument.do?navId=H0,C1,C1003,C1006,C1140,P19392,D13517
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/bitops.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. #include <linux/i2c/ltc4245.h>
  26. /* Here are names of the chip's registers (a.k.a. commands) */
  27. enum ltc4245_cmd {
  28. LTC4245_STATUS = 0x00, /* readonly */
  29. LTC4245_ALERT = 0x01,
  30. LTC4245_CONTROL = 0x02,
  31. LTC4245_ON = 0x03,
  32. LTC4245_FAULT1 = 0x04,
  33. LTC4245_FAULT2 = 0x05,
  34. LTC4245_GPIO = 0x06,
  35. LTC4245_ADCADR = 0x07,
  36. LTC4245_12VIN = 0x10,
  37. LTC4245_12VSENSE = 0x11,
  38. LTC4245_12VOUT = 0x12,
  39. LTC4245_5VIN = 0x13,
  40. LTC4245_5VSENSE = 0x14,
  41. LTC4245_5VOUT = 0x15,
  42. LTC4245_3VIN = 0x16,
  43. LTC4245_3VSENSE = 0x17,
  44. LTC4245_3VOUT = 0x18,
  45. LTC4245_VEEIN = 0x19,
  46. LTC4245_VEESENSE = 0x1a,
  47. LTC4245_VEEOUT = 0x1b,
  48. LTC4245_GPIOADC = 0x1c,
  49. };
  50. struct ltc4245_data {
  51. struct i2c_client *client;
  52. struct mutex update_lock;
  53. bool valid;
  54. unsigned long last_updated; /* in jiffies */
  55. /* Control registers */
  56. u8 cregs[0x08];
  57. /* Voltage registers */
  58. u8 vregs[0x0d];
  59. /* GPIO ADC registers */
  60. bool use_extra_gpios;
  61. int gpios[3];
  62. };
  63. /*
  64. * Update the readings from the GPIO pins. If the driver has been configured to
  65. * sample all GPIO's as analog voltages, a round-robin sampling method is used.
  66. * Otherwise, only the configured GPIO pin is sampled.
  67. *
  68. * LOCKING: must hold data->update_lock
  69. */
  70. static void ltc4245_update_gpios(struct device *dev)
  71. {
  72. struct ltc4245_data *data = dev_get_drvdata(dev);
  73. struct i2c_client *client = data->client;
  74. u8 gpio_curr, gpio_next, gpio_reg;
  75. int i;
  76. /* no extra gpio support, we're basically done */
  77. if (!data->use_extra_gpios) {
  78. data->gpios[0] = data->vregs[LTC4245_GPIOADC - 0x10];
  79. return;
  80. }
  81. /*
  82. * If the last reading was too long ago, then we mark all old GPIO
  83. * readings as stale by setting them to -EAGAIN
  84. */
  85. if (time_after(jiffies, data->last_updated + 5 * HZ)) {
  86. for (i = 0; i < ARRAY_SIZE(data->gpios); i++)
  87. data->gpios[i] = -EAGAIN;
  88. }
  89. /*
  90. * Get the current GPIO pin
  91. *
  92. * The datasheet calls these GPIO[1-3], but we'll calculate the zero
  93. * based array index instead, and call them GPIO[0-2]. This is much
  94. * easier to think about.
  95. */
  96. gpio_curr = (data->cregs[LTC4245_GPIO] & 0xc0) >> 6;
  97. if (gpio_curr > 0)
  98. gpio_curr -= 1;
  99. /* Read the GPIO voltage from the GPIOADC register */
  100. data->gpios[gpio_curr] = data->vregs[LTC4245_GPIOADC - 0x10];
  101. /* Find the next GPIO pin to read */
  102. gpio_next = (gpio_curr + 1) % ARRAY_SIZE(data->gpios);
  103. /*
  104. * Calculate the correct setting for the GPIO register so it will
  105. * sample the next GPIO pin
  106. */
  107. gpio_reg = (data->cregs[LTC4245_GPIO] & 0x3f) | ((gpio_next + 1) << 6);
  108. /* Update the GPIO register */
  109. i2c_smbus_write_byte_data(client, LTC4245_GPIO, gpio_reg);
  110. /* Update saved data */
  111. data->cregs[LTC4245_GPIO] = gpio_reg;
  112. }
  113. static struct ltc4245_data *ltc4245_update_device(struct device *dev)
  114. {
  115. struct ltc4245_data *data = dev_get_drvdata(dev);
  116. struct i2c_client *client = data->client;
  117. s32 val;
  118. int i;
  119. mutex_lock(&data->update_lock);
  120. if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
  121. /* Read control registers -- 0x00 to 0x07 */
  122. for (i = 0; i < ARRAY_SIZE(data->cregs); i++) {
  123. val = i2c_smbus_read_byte_data(client, i);
  124. if (unlikely(val < 0))
  125. data->cregs[i] = 0;
  126. else
  127. data->cregs[i] = val;
  128. }
  129. /* Read voltage registers -- 0x10 to 0x1c */
  130. for (i = 0; i < ARRAY_SIZE(data->vregs); i++) {
  131. val = i2c_smbus_read_byte_data(client, i+0x10);
  132. if (unlikely(val < 0))
  133. data->vregs[i] = 0;
  134. else
  135. data->vregs[i] = val;
  136. }
  137. /* Update GPIO readings */
  138. ltc4245_update_gpios(dev);
  139. data->last_updated = jiffies;
  140. data->valid = true;
  141. }
  142. mutex_unlock(&data->update_lock);
  143. return data;
  144. }
  145. /* Return the voltage from the given register in millivolts */
  146. static int ltc4245_get_voltage(struct device *dev, u8 reg)
  147. {
  148. struct ltc4245_data *data = ltc4245_update_device(dev);
  149. const u8 regval = data->vregs[reg - 0x10];
  150. u32 voltage = 0;
  151. switch (reg) {
  152. case LTC4245_12VIN:
  153. case LTC4245_12VOUT:
  154. voltage = regval * 55;
  155. break;
  156. case LTC4245_5VIN:
  157. case LTC4245_5VOUT:
  158. voltage = regval * 22;
  159. break;
  160. case LTC4245_3VIN:
  161. case LTC4245_3VOUT:
  162. voltage = regval * 15;
  163. break;
  164. case LTC4245_VEEIN:
  165. case LTC4245_VEEOUT:
  166. voltage = regval * -55;
  167. break;
  168. case LTC4245_GPIOADC:
  169. voltage = regval * 10;
  170. break;
  171. default:
  172. /* If we get here, the developer messed up */
  173. WARN_ON_ONCE(1);
  174. break;
  175. }
  176. return voltage;
  177. }
  178. /* Return the current in the given sense register in milliAmperes */
  179. static unsigned int ltc4245_get_current(struct device *dev, u8 reg)
  180. {
  181. struct ltc4245_data *data = ltc4245_update_device(dev);
  182. const u8 regval = data->vregs[reg - 0x10];
  183. unsigned int voltage;
  184. unsigned int curr;
  185. /*
  186. * The strange looking conversions that follow are fixed-point
  187. * math, since we cannot do floating point in the kernel.
  188. *
  189. * Step 1: convert sense register to microVolts
  190. * Step 2: convert voltage to milliAmperes
  191. *
  192. * If you play around with the V=IR equation, you come up with
  193. * the following: X uV / Y mOhm == Z mA
  194. *
  195. * With the resistors that are fractions of a milliOhm, we multiply
  196. * the voltage and resistance by 10, to shift the decimal point.
  197. * Now we can use the normal division operator again.
  198. */
  199. switch (reg) {
  200. case LTC4245_12VSENSE:
  201. voltage = regval * 250; /* voltage in uV */
  202. curr = voltage / 50; /* sense resistor 50 mOhm */
  203. break;
  204. case LTC4245_5VSENSE:
  205. voltage = regval * 125; /* voltage in uV */
  206. curr = (voltage * 10) / 35; /* sense resistor 3.5 mOhm */
  207. break;
  208. case LTC4245_3VSENSE:
  209. voltage = regval * 125; /* voltage in uV */
  210. curr = (voltage * 10) / 25; /* sense resistor 2.5 mOhm */
  211. break;
  212. case LTC4245_VEESENSE:
  213. voltage = regval * 250; /* voltage in uV */
  214. curr = voltage / 100; /* sense resistor 100 mOhm */
  215. break;
  216. default:
  217. /* If we get here, the developer messed up */
  218. WARN_ON_ONCE(1);
  219. curr = 0;
  220. break;
  221. }
  222. return curr;
  223. }
  224. /* Map from voltage channel index to voltage register */
  225. static const s8 ltc4245_in_regs[] = {
  226. LTC4245_12VIN, LTC4245_5VIN, LTC4245_3VIN, LTC4245_VEEIN,
  227. LTC4245_12VOUT, LTC4245_5VOUT, LTC4245_3VOUT, LTC4245_VEEOUT,
  228. };
  229. /* Map from current channel index to current register */
  230. static const s8 ltc4245_curr_regs[] = {
  231. LTC4245_12VSENSE, LTC4245_5VSENSE, LTC4245_3VSENSE, LTC4245_VEESENSE,
  232. };
  233. static int ltc4245_read_curr(struct device *dev, u32 attr, int channel,
  234. long *val)
  235. {
  236. struct ltc4245_data *data = ltc4245_update_device(dev);
  237. switch (attr) {
  238. case hwmon_curr_input:
  239. *val = ltc4245_get_current(dev, ltc4245_curr_regs[channel]);
  240. return 0;
  241. case hwmon_curr_max_alarm:
  242. *val = !!(data->cregs[LTC4245_FAULT1] & BIT(channel + 4));
  243. return 0;
  244. default:
  245. return -EOPNOTSUPP;
  246. }
  247. }
  248. static int ltc4245_read_in(struct device *dev, u32 attr, int channel, long *val)
  249. {
  250. struct ltc4245_data *data = ltc4245_update_device(dev);
  251. switch (attr) {
  252. case hwmon_in_input:
  253. if (channel < 8) {
  254. *val = ltc4245_get_voltage(dev,
  255. ltc4245_in_regs[channel]);
  256. } else {
  257. int regval = data->gpios[channel - 8];
  258. if (regval < 0)
  259. return regval;
  260. *val = regval * 10;
  261. }
  262. return 0;
  263. case hwmon_in_min_alarm:
  264. if (channel < 4)
  265. *val = !!(data->cregs[LTC4245_FAULT1] & BIT(channel));
  266. else
  267. *val = !!(data->cregs[LTC4245_FAULT2] &
  268. BIT(channel - 4));
  269. return 0;
  270. default:
  271. return -EOPNOTSUPP;
  272. }
  273. }
  274. static int ltc4245_read_power(struct device *dev, u32 attr, int channel,
  275. long *val)
  276. {
  277. unsigned long curr;
  278. long voltage;
  279. switch (attr) {
  280. case hwmon_power_input:
  281. (void)ltc4245_update_device(dev);
  282. curr = ltc4245_get_current(dev, ltc4245_curr_regs[channel]);
  283. voltage = ltc4245_get_voltage(dev, ltc4245_in_regs[channel]);
  284. *val = abs(curr * voltage);
  285. return 0;
  286. default:
  287. return -EOPNOTSUPP;
  288. }
  289. }
  290. static int ltc4245_read(struct device *dev, enum hwmon_sensor_types type,
  291. u32 attr, int channel, long *val)
  292. {
  293. switch (type) {
  294. case hwmon_curr:
  295. return ltc4245_read_curr(dev, attr, channel, val);
  296. case hwmon_power:
  297. return ltc4245_read_power(dev, attr, channel, val);
  298. case hwmon_in:
  299. return ltc4245_read_in(dev, attr, channel - 1, val);
  300. default:
  301. return -EOPNOTSUPP;
  302. }
  303. }
  304. static umode_t ltc4245_is_visible(const void *_data,
  305. enum hwmon_sensor_types type,
  306. u32 attr, int channel)
  307. {
  308. const struct ltc4245_data *data = _data;
  309. switch (type) {
  310. case hwmon_in:
  311. if (channel == 0)
  312. return 0;
  313. switch (attr) {
  314. case hwmon_in_input:
  315. if (channel > 9 && !data->use_extra_gpios)
  316. return 0;
  317. return S_IRUGO;
  318. case hwmon_in_min_alarm:
  319. if (channel > 8)
  320. return 0;
  321. return S_IRUGO;
  322. default:
  323. return 0;
  324. }
  325. case hwmon_curr:
  326. switch (attr) {
  327. case hwmon_curr_input:
  328. case hwmon_curr_max_alarm:
  329. return S_IRUGO;
  330. default:
  331. return 0;
  332. }
  333. case hwmon_power:
  334. switch (attr) {
  335. case hwmon_power_input:
  336. return S_IRUGO;
  337. default:
  338. return 0;
  339. }
  340. default:
  341. return 0;
  342. }
  343. }
  344. static const u32 ltc4245_in_config[] = {
  345. HWMON_I_INPUT, /* dummy, skipped in is_visible */
  346. HWMON_I_INPUT | HWMON_I_MIN_ALARM,
  347. HWMON_I_INPUT | HWMON_I_MIN_ALARM,
  348. HWMON_I_INPUT | HWMON_I_MIN_ALARM,
  349. HWMON_I_INPUT | HWMON_I_MIN_ALARM,
  350. HWMON_I_INPUT | HWMON_I_MIN_ALARM,
  351. HWMON_I_INPUT | HWMON_I_MIN_ALARM,
  352. HWMON_I_INPUT | HWMON_I_MIN_ALARM,
  353. HWMON_I_INPUT | HWMON_I_MIN_ALARM,
  354. HWMON_I_INPUT,
  355. HWMON_I_INPUT,
  356. HWMON_I_INPUT,
  357. 0
  358. };
  359. static const struct hwmon_channel_info ltc4245_in = {
  360. .type = hwmon_in,
  361. .config = ltc4245_in_config,
  362. };
  363. static const u32 ltc4245_curr_config[] = {
  364. HWMON_C_INPUT | HWMON_C_MAX_ALARM,
  365. HWMON_C_INPUT | HWMON_C_MAX_ALARM,
  366. HWMON_C_INPUT | HWMON_C_MAX_ALARM,
  367. HWMON_C_INPUT | HWMON_C_MAX_ALARM,
  368. 0
  369. };
  370. static const struct hwmon_channel_info ltc4245_curr = {
  371. .type = hwmon_curr,
  372. .config = ltc4245_curr_config,
  373. };
  374. static const u32 ltc4245_power_config[] = {
  375. HWMON_P_INPUT,
  376. HWMON_P_INPUT,
  377. HWMON_P_INPUT,
  378. HWMON_P_INPUT,
  379. 0
  380. };
  381. static const struct hwmon_channel_info ltc4245_power = {
  382. .type = hwmon_power,
  383. .config = ltc4245_power_config,
  384. };
  385. static const struct hwmon_channel_info *ltc4245_info[] = {
  386. &ltc4245_in,
  387. &ltc4245_curr,
  388. &ltc4245_power,
  389. NULL
  390. };
  391. static const struct hwmon_ops ltc4245_hwmon_ops = {
  392. .is_visible = ltc4245_is_visible,
  393. .read = ltc4245_read,
  394. };
  395. static const struct hwmon_chip_info ltc4245_chip_info = {
  396. .ops = &ltc4245_hwmon_ops,
  397. .info = ltc4245_info,
  398. };
  399. static bool ltc4245_use_extra_gpios(struct i2c_client *client)
  400. {
  401. struct ltc4245_platform_data *pdata = dev_get_platdata(&client->dev);
  402. struct device_node *np = client->dev.of_node;
  403. /* prefer platform data */
  404. if (pdata)
  405. return pdata->use_extra_gpios;
  406. /* fallback on OF */
  407. if (of_find_property(np, "ltc4245,use-extra-gpios", NULL))
  408. return true;
  409. return false;
  410. }
  411. static int ltc4245_probe(struct i2c_client *client,
  412. const struct i2c_device_id *id)
  413. {
  414. struct i2c_adapter *adapter = client->adapter;
  415. struct ltc4245_data *data;
  416. struct device *hwmon_dev;
  417. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  418. return -ENODEV;
  419. data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
  420. if (!data)
  421. return -ENOMEM;
  422. data->client = client;
  423. mutex_init(&data->update_lock);
  424. data->use_extra_gpios = ltc4245_use_extra_gpios(client);
  425. /* Initialize the LTC4245 chip */
  426. i2c_smbus_write_byte_data(client, LTC4245_FAULT1, 0x00);
  427. i2c_smbus_write_byte_data(client, LTC4245_FAULT2, 0x00);
  428. hwmon_dev = devm_hwmon_device_register_with_info(&client->dev,
  429. client->name, data,
  430. &ltc4245_chip_info,
  431. NULL);
  432. return PTR_ERR_OR_ZERO(hwmon_dev);
  433. }
  434. static const struct i2c_device_id ltc4245_id[] = {
  435. { "ltc4245", 0 },
  436. { }
  437. };
  438. MODULE_DEVICE_TABLE(i2c, ltc4245_id);
  439. /* This is the driver that will be inserted */
  440. static struct i2c_driver ltc4245_driver = {
  441. .driver = {
  442. .name = "ltc4245",
  443. },
  444. .probe = ltc4245_probe,
  445. .id_table = ltc4245_id,
  446. };
  447. module_i2c_driver(ltc4245_driver);
  448. MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
  449. MODULE_DESCRIPTION("LTC4245 driver");
  450. MODULE_LICENSE("GPL");