i2c-smbus.c 7.1 KB

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