ltc4245.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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/err.h>
  19. #include <linux/slab.h>
  20. #include <linux/i2c.h>
  21. #include <linux/hwmon.h>
  22. #include <linux/hwmon-sysfs.h>
  23. #include <linux/jiffies.h>
  24. #include <linux/i2c/ltc4245.h>
  25. /* Here are names of the chip's registers (a.k.a. commands) */
  26. enum ltc4245_cmd {
  27. LTC4245_STATUS = 0x00, /* readonly */
  28. LTC4245_ALERT = 0x01,
  29. LTC4245_CONTROL = 0x02,
  30. LTC4245_ON = 0x03,
  31. LTC4245_FAULT1 = 0x04,
  32. LTC4245_FAULT2 = 0x05,
  33. LTC4245_GPIO = 0x06,
  34. LTC4245_ADCADR = 0x07,
  35. LTC4245_12VIN = 0x10,
  36. LTC4245_12VSENSE = 0x11,
  37. LTC4245_12VOUT = 0x12,
  38. LTC4245_5VIN = 0x13,
  39. LTC4245_5VSENSE = 0x14,
  40. LTC4245_5VOUT = 0x15,
  41. LTC4245_3VIN = 0x16,
  42. LTC4245_3VSENSE = 0x17,
  43. LTC4245_3VOUT = 0x18,
  44. LTC4245_VEEIN = 0x19,
  45. LTC4245_VEESENSE = 0x1a,
  46. LTC4245_VEEOUT = 0x1b,
  47. LTC4245_GPIOADC = 0x1c,
  48. };
  49. struct ltc4245_data {
  50. struct device *hwmon_dev;
  51. struct mutex update_lock;
  52. bool valid;
  53. unsigned long last_updated; /* in jiffies */
  54. /* Control registers */
  55. u8 cregs[0x08];
  56. /* Voltage registers */
  57. u8 vregs[0x0d];
  58. /* GPIO ADC registers */
  59. bool use_extra_gpios;
  60. int gpios[3];
  61. };
  62. /*
  63. * Update the readings from the GPIO pins. If the driver has been configured to
  64. * sample all GPIO's as analog voltages, a round-robin sampling method is used.
  65. * Otherwise, only the configured GPIO pin is sampled.
  66. *
  67. * LOCKING: must hold data->update_lock
  68. */
  69. static void ltc4245_update_gpios(struct device *dev)
  70. {
  71. struct i2c_client *client = to_i2c_client(dev);
  72. struct ltc4245_data *data = i2c_get_clientdata(client);
  73. u8 gpio_curr, gpio_next, gpio_reg;
  74. int i;
  75. /* no extra gpio support, we're basically done */
  76. if (!data->use_extra_gpios) {
  77. data->gpios[0] = data->vregs[LTC4245_GPIOADC - 0x10];
  78. return;
  79. }
  80. /*
  81. * If the last reading was too long ago, then we mark all old GPIO
  82. * readings as stale by setting them to -EAGAIN
  83. */
  84. if (time_after(jiffies, data->last_updated + 5 * HZ)) {
  85. dev_dbg(&client->dev, "Marking GPIOs invalid\n");
  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 i2c_client *client = to_i2c_client(dev);
  116. struct ltc4245_data *data = i2c_get_clientdata(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. dev_dbg(&client->dev, "Starting ltc4245 update\n");
  122. /* Read control registers -- 0x00 to 0x07 */
  123. for (i = 0; i < ARRAY_SIZE(data->cregs); i++) {
  124. val = i2c_smbus_read_byte_data(client, i);
  125. if (unlikely(val < 0))
  126. data->cregs[i] = 0;
  127. else
  128. data->cregs[i] = val;
  129. }
  130. /* Read voltage registers -- 0x10 to 0x1c */
  131. for (i = 0; i < ARRAY_SIZE(data->vregs); i++) {
  132. val = i2c_smbus_read_byte_data(client, i+0x10);
  133. if (unlikely(val < 0))
  134. data->vregs[i] = 0;
  135. else
  136. data->vregs[i] = val;
  137. }
  138. /* Update GPIO readings */
  139. ltc4245_update_gpios(dev);
  140. data->last_updated = jiffies;
  141. data->valid = 1;
  142. }
  143. mutex_unlock(&data->update_lock);
  144. return data;
  145. }
  146. /* Return the voltage from the given register in millivolts */
  147. static int ltc4245_get_voltage(struct device *dev, u8 reg)
  148. {
  149. struct ltc4245_data *data = ltc4245_update_device(dev);
  150. const u8 regval = data->vregs[reg - 0x10];
  151. u32 voltage = 0;
  152. switch (reg) {
  153. case LTC4245_12VIN:
  154. case LTC4245_12VOUT:
  155. voltage = regval * 55;
  156. break;
  157. case LTC4245_5VIN:
  158. case LTC4245_5VOUT:
  159. voltage = regval * 22;
  160. break;
  161. case LTC4245_3VIN:
  162. case LTC4245_3VOUT:
  163. voltage = regval * 15;
  164. break;
  165. case LTC4245_VEEIN:
  166. case LTC4245_VEEOUT:
  167. voltage = regval * -55;
  168. break;
  169. case LTC4245_GPIOADC:
  170. voltage = regval * 10;
  171. break;
  172. default:
  173. /* If we get here, the developer messed up */
  174. WARN_ON_ONCE(1);
  175. break;
  176. }
  177. return voltage;
  178. }
  179. /* Return the current in the given sense register in milliAmperes */
  180. static unsigned int ltc4245_get_current(struct device *dev, u8 reg)
  181. {
  182. struct ltc4245_data *data = ltc4245_update_device(dev);
  183. const u8 regval = data->vregs[reg - 0x10];
  184. unsigned int voltage;
  185. unsigned int curr;
  186. /*
  187. * The strange looking conversions that follow are fixed-point
  188. * math, since we cannot do floating point in the kernel.
  189. *
  190. * Step 1: convert sense register to microVolts
  191. * Step 2: convert voltage to milliAmperes
  192. *
  193. * If you play around with the V=IR equation, you come up with
  194. * the following: X uV / Y mOhm == Z mA
  195. *
  196. * With the resistors that are fractions of a milliOhm, we multiply
  197. * the voltage and resistance by 10, to shift the decimal point.
  198. * Now we can use the normal division operator again.
  199. */
  200. switch (reg) {
  201. case LTC4245_12VSENSE:
  202. voltage = regval * 250; /* voltage in uV */
  203. curr = voltage / 50; /* sense resistor 50 mOhm */
  204. break;
  205. case LTC4245_5VSENSE:
  206. voltage = regval * 125; /* voltage in uV */
  207. curr = (voltage * 10) / 35; /* sense resistor 3.5 mOhm */
  208. break;
  209. case LTC4245_3VSENSE:
  210. voltage = regval * 125; /* voltage in uV */
  211. curr = (voltage * 10) / 25; /* sense resistor 2.5 mOhm */
  212. break;
  213. case LTC4245_VEESENSE:
  214. voltage = regval * 250; /* voltage in uV */
  215. curr = voltage / 100; /* sense resistor 100 mOhm */
  216. break;
  217. default:
  218. /* If we get here, the developer messed up */
  219. WARN_ON_ONCE(1);
  220. curr = 0;
  221. break;
  222. }
  223. return curr;
  224. }
  225. static ssize_t ltc4245_show_voltage(struct device *dev,
  226. struct device_attribute *da,
  227. char *buf)
  228. {
  229. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  230. const int voltage = ltc4245_get_voltage(dev, attr->index);
  231. return snprintf(buf, PAGE_SIZE, "%d\n", voltage);
  232. }
  233. static ssize_t ltc4245_show_current(struct device *dev,
  234. struct device_attribute *da,
  235. char *buf)
  236. {
  237. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  238. const unsigned int curr = ltc4245_get_current(dev, attr->index);
  239. return snprintf(buf, PAGE_SIZE, "%u\n", curr);
  240. }
  241. static ssize_t ltc4245_show_power(struct device *dev,
  242. struct device_attribute *da,
  243. char *buf)
  244. {
  245. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  246. const unsigned int curr = ltc4245_get_current(dev, attr->index);
  247. const int output_voltage = ltc4245_get_voltage(dev, attr->index+1);
  248. /* current in mA * voltage in mV == power in uW */
  249. const unsigned int power = abs(output_voltage * curr);
  250. return snprintf(buf, PAGE_SIZE, "%u\n", power);
  251. }
  252. static ssize_t ltc4245_show_alarm(struct device *dev,
  253. struct device_attribute *da,
  254. char *buf)
  255. {
  256. struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(da);
  257. struct ltc4245_data *data = ltc4245_update_device(dev);
  258. const u8 reg = data->cregs[attr->index];
  259. const u32 mask = attr->nr;
  260. return snprintf(buf, PAGE_SIZE, "%u\n", (reg & mask) ? 1 : 0);
  261. }
  262. static ssize_t ltc4245_show_gpio(struct device *dev,
  263. struct device_attribute *da,
  264. char *buf)
  265. {
  266. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  267. struct ltc4245_data *data = ltc4245_update_device(dev);
  268. int val = data->gpios[attr->index];
  269. /* handle stale GPIO's */
  270. if (val < 0)
  271. return val;
  272. /* Convert to millivolts and print */
  273. return snprintf(buf, PAGE_SIZE, "%u\n", val * 10);
  274. }
  275. /* Construct a sensor_device_attribute structure for each register */
  276. /* Input voltages */
  277. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, ltc4245_show_voltage, NULL,
  278. LTC4245_12VIN);
  279. static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, ltc4245_show_voltage, NULL,
  280. LTC4245_5VIN);
  281. static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, ltc4245_show_voltage, NULL,
  282. LTC4245_3VIN);
  283. static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, ltc4245_show_voltage, NULL,
  284. LTC4245_VEEIN);
  285. /* Input undervoltage alarms */
  286. static SENSOR_DEVICE_ATTR_2(in1_min_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
  287. 1 << 0, LTC4245_FAULT1);
  288. static SENSOR_DEVICE_ATTR_2(in2_min_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
  289. 1 << 1, LTC4245_FAULT1);
  290. static SENSOR_DEVICE_ATTR_2(in3_min_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
  291. 1 << 2, LTC4245_FAULT1);
  292. static SENSOR_DEVICE_ATTR_2(in4_min_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
  293. 1 << 3, LTC4245_FAULT1);
  294. /* Currents (via sense resistor) */
  295. static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, ltc4245_show_current, NULL,
  296. LTC4245_12VSENSE);
  297. static SENSOR_DEVICE_ATTR(curr2_input, S_IRUGO, ltc4245_show_current, NULL,
  298. LTC4245_5VSENSE);
  299. static SENSOR_DEVICE_ATTR(curr3_input, S_IRUGO, ltc4245_show_current, NULL,
  300. LTC4245_3VSENSE);
  301. static SENSOR_DEVICE_ATTR(curr4_input, S_IRUGO, ltc4245_show_current, NULL,
  302. LTC4245_VEESENSE);
  303. /* Overcurrent alarms */
  304. static SENSOR_DEVICE_ATTR_2(curr1_max_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
  305. 1 << 4, LTC4245_FAULT1);
  306. static SENSOR_DEVICE_ATTR_2(curr2_max_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
  307. 1 << 5, LTC4245_FAULT1);
  308. static SENSOR_DEVICE_ATTR_2(curr3_max_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
  309. 1 << 6, LTC4245_FAULT1);
  310. static SENSOR_DEVICE_ATTR_2(curr4_max_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
  311. 1 << 7, LTC4245_FAULT1);
  312. /* Output voltages */
  313. static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, ltc4245_show_voltage, NULL,
  314. LTC4245_12VOUT);
  315. static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, ltc4245_show_voltage, NULL,
  316. LTC4245_5VOUT);
  317. static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, ltc4245_show_voltage, NULL,
  318. LTC4245_3VOUT);
  319. static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, ltc4245_show_voltage, NULL,
  320. LTC4245_VEEOUT);
  321. /* Power Bad alarms */
  322. static SENSOR_DEVICE_ATTR_2(in5_min_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
  323. 1 << 0, LTC4245_FAULT2);
  324. static SENSOR_DEVICE_ATTR_2(in6_min_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
  325. 1 << 1, LTC4245_FAULT2);
  326. static SENSOR_DEVICE_ATTR_2(in7_min_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
  327. 1 << 2, LTC4245_FAULT2);
  328. static SENSOR_DEVICE_ATTR_2(in8_min_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
  329. 1 << 3, LTC4245_FAULT2);
  330. /* GPIO voltages */
  331. static SENSOR_DEVICE_ATTR(in9_input, S_IRUGO, ltc4245_show_gpio, NULL, 0);
  332. static SENSOR_DEVICE_ATTR(in10_input, S_IRUGO, ltc4245_show_gpio, NULL, 1);
  333. static SENSOR_DEVICE_ATTR(in11_input, S_IRUGO, ltc4245_show_gpio, NULL, 2);
  334. /* Power Consumption (virtual) */
  335. static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, ltc4245_show_power, NULL,
  336. LTC4245_12VSENSE);
  337. static SENSOR_DEVICE_ATTR(power2_input, S_IRUGO, ltc4245_show_power, NULL,
  338. LTC4245_5VSENSE);
  339. static SENSOR_DEVICE_ATTR(power3_input, S_IRUGO, ltc4245_show_power, NULL,
  340. LTC4245_3VSENSE);
  341. static SENSOR_DEVICE_ATTR(power4_input, S_IRUGO, ltc4245_show_power, NULL,
  342. LTC4245_VEESENSE);
  343. /*
  344. * Finally, construct an array of pointers to members of the above objects,
  345. * as required for sysfs_create_group()
  346. */
  347. static struct attribute *ltc4245_std_attributes[] = {
  348. &sensor_dev_attr_in1_input.dev_attr.attr,
  349. &sensor_dev_attr_in2_input.dev_attr.attr,
  350. &sensor_dev_attr_in3_input.dev_attr.attr,
  351. &sensor_dev_attr_in4_input.dev_attr.attr,
  352. &sensor_dev_attr_in1_min_alarm.dev_attr.attr,
  353. &sensor_dev_attr_in2_min_alarm.dev_attr.attr,
  354. &sensor_dev_attr_in3_min_alarm.dev_attr.attr,
  355. &sensor_dev_attr_in4_min_alarm.dev_attr.attr,
  356. &sensor_dev_attr_curr1_input.dev_attr.attr,
  357. &sensor_dev_attr_curr2_input.dev_attr.attr,
  358. &sensor_dev_attr_curr3_input.dev_attr.attr,
  359. &sensor_dev_attr_curr4_input.dev_attr.attr,
  360. &sensor_dev_attr_curr1_max_alarm.dev_attr.attr,
  361. &sensor_dev_attr_curr2_max_alarm.dev_attr.attr,
  362. &sensor_dev_attr_curr3_max_alarm.dev_attr.attr,
  363. &sensor_dev_attr_curr4_max_alarm.dev_attr.attr,
  364. &sensor_dev_attr_in5_input.dev_attr.attr,
  365. &sensor_dev_attr_in6_input.dev_attr.attr,
  366. &sensor_dev_attr_in7_input.dev_attr.attr,
  367. &sensor_dev_attr_in8_input.dev_attr.attr,
  368. &sensor_dev_attr_in5_min_alarm.dev_attr.attr,
  369. &sensor_dev_attr_in6_min_alarm.dev_attr.attr,
  370. &sensor_dev_attr_in7_min_alarm.dev_attr.attr,
  371. &sensor_dev_attr_in8_min_alarm.dev_attr.attr,
  372. &sensor_dev_attr_in9_input.dev_attr.attr,
  373. &sensor_dev_attr_power1_input.dev_attr.attr,
  374. &sensor_dev_attr_power2_input.dev_attr.attr,
  375. &sensor_dev_attr_power3_input.dev_attr.attr,
  376. &sensor_dev_attr_power4_input.dev_attr.attr,
  377. NULL,
  378. };
  379. static struct attribute *ltc4245_gpio_attributes[] = {
  380. &sensor_dev_attr_in10_input.dev_attr.attr,
  381. &sensor_dev_attr_in11_input.dev_attr.attr,
  382. NULL,
  383. };
  384. static const struct attribute_group ltc4245_std_group = {
  385. .attrs = ltc4245_std_attributes,
  386. };
  387. static const struct attribute_group ltc4245_gpio_group = {
  388. .attrs = ltc4245_gpio_attributes,
  389. };
  390. static int ltc4245_sysfs_create_groups(struct i2c_client *client)
  391. {
  392. struct ltc4245_data *data = i2c_get_clientdata(client);
  393. struct device *dev = &client->dev;
  394. int ret;
  395. /* register the standard sysfs attributes */
  396. ret = sysfs_create_group(&dev->kobj, &ltc4245_std_group);
  397. if (ret) {
  398. dev_err(dev, "unable to register standard attributes\n");
  399. return ret;
  400. }
  401. /* if we're using the extra gpio support, register it's attributes */
  402. if (data->use_extra_gpios) {
  403. ret = sysfs_create_group(&dev->kobj, &ltc4245_gpio_group);
  404. if (ret) {
  405. dev_err(dev, "unable to register gpio attributes\n");
  406. sysfs_remove_group(&dev->kobj, &ltc4245_std_group);
  407. return ret;
  408. }
  409. }
  410. return 0;
  411. }
  412. static void ltc4245_sysfs_remove_groups(struct i2c_client *client)
  413. {
  414. struct ltc4245_data *data = i2c_get_clientdata(client);
  415. struct device *dev = &client->dev;
  416. if (data->use_extra_gpios)
  417. sysfs_remove_group(&dev->kobj, &ltc4245_gpio_group);
  418. sysfs_remove_group(&dev->kobj, &ltc4245_std_group);
  419. }
  420. static bool ltc4245_use_extra_gpios(struct i2c_client *client)
  421. {
  422. struct ltc4245_platform_data *pdata = dev_get_platdata(&client->dev);
  423. #ifdef CONFIG_OF
  424. struct device_node *np = client->dev.of_node;
  425. #endif
  426. /* prefer platform data */
  427. if (pdata)
  428. return pdata->use_extra_gpios;
  429. #ifdef CONFIG_OF
  430. /* fallback on OF */
  431. if (of_find_property(np, "ltc4245,use-extra-gpios", NULL))
  432. return true;
  433. #endif
  434. return false;
  435. }
  436. static int ltc4245_probe(struct i2c_client *client,
  437. const struct i2c_device_id *id)
  438. {
  439. struct i2c_adapter *adapter = client->adapter;
  440. struct ltc4245_data *data;
  441. int ret;
  442. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  443. return -ENODEV;
  444. data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
  445. if (!data)
  446. return -ENOMEM;
  447. i2c_set_clientdata(client, data);
  448. mutex_init(&data->update_lock);
  449. data->use_extra_gpios = ltc4245_use_extra_gpios(client);
  450. /* Initialize the LTC4245 chip */
  451. i2c_smbus_write_byte_data(client, LTC4245_FAULT1, 0x00);
  452. i2c_smbus_write_byte_data(client, LTC4245_FAULT2, 0x00);
  453. /* Register sysfs hooks */
  454. ret = ltc4245_sysfs_create_groups(client);
  455. if (ret)
  456. return ret;
  457. data->hwmon_dev = hwmon_device_register(&client->dev);
  458. if (IS_ERR(data->hwmon_dev)) {
  459. ret = PTR_ERR(data->hwmon_dev);
  460. goto out_hwmon_device_register;
  461. }
  462. return 0;
  463. out_hwmon_device_register:
  464. ltc4245_sysfs_remove_groups(client);
  465. return ret;
  466. }
  467. static int ltc4245_remove(struct i2c_client *client)
  468. {
  469. struct ltc4245_data *data = i2c_get_clientdata(client);
  470. hwmon_device_unregister(data->hwmon_dev);
  471. ltc4245_sysfs_remove_groups(client);
  472. return 0;
  473. }
  474. static const struct i2c_device_id ltc4245_id[] = {
  475. { "ltc4245", 0 },
  476. { }
  477. };
  478. MODULE_DEVICE_TABLE(i2c, ltc4245_id);
  479. /* This is the driver that will be inserted */
  480. static struct i2c_driver ltc4245_driver = {
  481. .driver = {
  482. .name = "ltc4245",
  483. },
  484. .probe = ltc4245_probe,
  485. .remove = ltc4245_remove,
  486. .id_table = ltc4245_id,
  487. };
  488. module_i2c_driver(ltc4245_driver);
  489. MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
  490. MODULE_DESCRIPTION("LTC4245 driver");
  491. MODULE_LICENSE("GPL");