i2c-smbus.c 7.1 KB

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