i2c-smbus.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * i2c-smbus.c - SMBus extensions to the I2C protocol
  3. *
  4. * Copyright (C) 2008 David Brownell
  5. * Copyright (C) 2010 Jean Delvare <jdelvare@suse.de>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/device.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/workqueue.h>
  22. #include <linux/i2c.h>
  23. #include <linux/i2c-smbus.h>
  24. #include <linux/slab.h>
  25. struct i2c_smbus_alert {
  26. unsigned int alert_edge_triggered:1;
  27. int irq;
  28. struct work_struct alert;
  29. struct i2c_client *ara; /* Alert response address */
  30. };
  31. struct alert_data {
  32. unsigned short addr;
  33. u8 flag:1;
  34. };
  35. /* If this is the alerting device, notify its driver */
  36. static int smbus_do_alert(struct device *dev, void *addrp)
  37. {
  38. struct i2c_client *client = i2c_verify_client(dev);
  39. struct alert_data *data = addrp;
  40. struct i2c_driver *driver;
  41. if (!client || client->addr != data->addr)
  42. return 0;
  43. if (client->flags & I2C_CLIENT_TEN)
  44. return 0;
  45. /*
  46. * Drivers should either disable alerts, or provide at least
  47. * a minimal handler. Lock so the driver won't change.
  48. */
  49. device_lock(dev);
  50. if (client->dev.driver) {
  51. driver = to_i2c_driver(client->dev.driver);
  52. if (driver->alert)
  53. driver->alert(client, data->flag);
  54. else
  55. dev_warn(&client->dev, "no driver alert()!\n");
  56. } else
  57. dev_dbg(&client->dev, "alert with no driver\n");
  58. device_unlock(dev);
  59. /* Stop iterating after we find the device */
  60. return -EBUSY;
  61. }
  62. /*
  63. * The alert IRQ handler needs to hand work off to a task which can issue
  64. * SMBus calls, because those sleeping calls can't be made in IRQ context.
  65. */
  66. static void smbus_alert(struct work_struct *work)
  67. {
  68. struct i2c_smbus_alert *alert;
  69. struct i2c_client *ara;
  70. unsigned short prev_addr = 0; /* Not a valid address */
  71. alert = container_of(work, struct i2c_smbus_alert, alert);
  72. ara = alert->ara;
  73. for (;;) {
  74. s32 status;
  75. struct alert_data data;
  76. /*
  77. * Devices with pending alerts reply in address order, low
  78. * to high, because of slave transmit arbitration. After
  79. * responding, an SMBus device stops asserting SMBALERT#.
  80. *
  81. * Note that SMBus 2.0 reserves 10-bit addresses for future
  82. * use. We neither handle them, nor try to use PEC here.
  83. */
  84. status = i2c_smbus_read_byte(ara);
  85. if (status < 0)
  86. break;
  87. data.flag = status & 1;
  88. data.addr = status >> 1;
  89. if (data.addr == prev_addr) {
  90. dev_warn(&ara->dev, "Duplicate SMBALERT# from dev "
  91. "0x%02x, skipping\n", data.addr);
  92. break;
  93. }
  94. dev_dbg(&ara->dev, "SMBALERT# from dev 0x%02x, flag %d\n",
  95. data.addr, data.flag);
  96. /* Notify driver for the device which issued the alert */
  97. device_for_each_child(&ara->adapter->dev, &data,
  98. smbus_do_alert);
  99. prev_addr = data.addr;
  100. }
  101. /* We handled all alerts; re-enable level-triggered IRQs */
  102. if (!alert->alert_edge_triggered)
  103. enable_irq(alert->irq);
  104. }
  105. static irqreturn_t smbalert_irq(int irq, void *d)
  106. {
  107. struct i2c_smbus_alert *alert = d;
  108. /* Disable level-triggered IRQs until we handle them */
  109. if (!alert->alert_edge_triggered)
  110. disable_irq_nosync(irq);
  111. schedule_work(&alert->alert);
  112. return IRQ_HANDLED;
  113. }
  114. /* Setup SMBALERT# infrastructure */
  115. static int smbalert_probe(struct i2c_client *ara,
  116. const struct i2c_device_id *id)
  117. {
  118. struct i2c_smbus_alert_setup *setup = dev_get_platdata(&ara->dev);
  119. struct i2c_smbus_alert *alert;
  120. struct i2c_adapter *adapter = ara->adapter;
  121. int res;
  122. alert = devm_kzalloc(&ara->dev, sizeof(struct i2c_smbus_alert),
  123. GFP_KERNEL);
  124. if (!alert)
  125. return -ENOMEM;
  126. alert->alert_edge_triggered = setup->alert_edge_triggered;
  127. alert->irq = setup->irq;
  128. INIT_WORK(&alert->alert, smbus_alert);
  129. alert->ara = ara;
  130. if (setup->irq > 0) {
  131. res = devm_request_irq(&ara->dev, setup->irq, smbalert_irq,
  132. 0, "smbus_alert", alert);
  133. if (res)
  134. return res;
  135. }
  136. i2c_set_clientdata(ara, alert);
  137. dev_info(&adapter->dev, "supports SMBALERT#, %s trigger\n",
  138. setup->alert_edge_triggered ? "edge" : "level");
  139. return 0;
  140. }
  141. /* IRQ and memory resources are managed so they are freed automatically */
  142. static int smbalert_remove(struct i2c_client *ara)
  143. {
  144. struct i2c_smbus_alert *alert = i2c_get_clientdata(ara);
  145. cancel_work_sync(&alert->alert);
  146. return 0;
  147. }
  148. static const struct i2c_device_id smbalert_ids[] = {
  149. { "smbus_alert", 0 },
  150. { /* LIST END */ }
  151. };
  152. MODULE_DEVICE_TABLE(i2c, smbalert_ids);
  153. static struct i2c_driver smbalert_driver = {
  154. .driver = {
  155. .name = "smbus_alert",
  156. },
  157. .probe = smbalert_probe,
  158. .remove = smbalert_remove,
  159. .id_table = smbalert_ids,
  160. };
  161. /**
  162. * i2c_setup_smbus_alert - Setup SMBus alert support
  163. * @adapter: the target adapter
  164. * @setup: setup data for the SMBus alert handler
  165. * Context: can sleep
  166. *
  167. * Setup handling of the SMBus alert protocol on a given I2C bus segment.
  168. *
  169. * Handling can be done either through our IRQ handler, or by the
  170. * adapter (from its handler, periodic polling, or whatever).
  171. *
  172. * NOTE that if we manage the IRQ, we *MUST* know if it's level or
  173. * edge triggered in order to hand it to the workqueue correctly.
  174. * If triggering the alert seems to wedge the system, you probably
  175. * should have said it's level triggered.
  176. *
  177. * This returns the ara client, which should be saved for later use with
  178. * i2c_handle_smbus_alert() and ultimately i2c_unregister_device(); or NULL
  179. * to indicate an error.
  180. */
  181. struct i2c_client *i2c_setup_smbus_alert(struct i2c_adapter *adapter,
  182. struct i2c_smbus_alert_setup *setup)
  183. {
  184. struct i2c_board_info ara_board_info = {
  185. I2C_BOARD_INFO("smbus_alert", 0x0c),
  186. .platform_data = setup,
  187. };
  188. return i2c_new_device(adapter, &ara_board_info);
  189. }
  190. EXPORT_SYMBOL_GPL(i2c_setup_smbus_alert);
  191. /**
  192. * i2c_handle_smbus_alert - Handle an SMBus alert
  193. * @ara: the ARA client on the relevant adapter
  194. * Context: can't sleep
  195. *
  196. * Helper function to be called from an I2C bus driver's interrupt
  197. * handler. It will schedule the alert work, in turn calling the
  198. * corresponding I2C device driver's alert function.
  199. *
  200. * It is assumed that ara is a valid i2c client previously returned by
  201. * i2c_setup_smbus_alert().
  202. */
  203. int i2c_handle_smbus_alert(struct i2c_client *ara)
  204. {
  205. struct i2c_smbus_alert *alert = i2c_get_clientdata(ara);
  206. return schedule_work(&alert->alert);
  207. }
  208. EXPORT_SYMBOL_GPL(i2c_handle_smbus_alert);
  209. module_i2c_driver(smbalert_driver);
  210. MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
  211. MODULE_DESCRIPTION("SMBus protocol extensions support");
  212. MODULE_LICENSE("GPL");