i2c-smbus.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 = devm_kzalloc(&ara->dev, sizeof(struct i2c_smbus_alert),
  126. GFP_KERNEL);
  127. if (!alert)
  128. return -ENOMEM;
  129. alert->alert_edge_triggered = setup->alert_edge_triggered;
  130. alert->irq = setup->irq;
  131. INIT_WORK(&alert->alert, smbus_alert);
  132. alert->ara = ara;
  133. if (setup->irq > 0) {
  134. res = devm_request_irq(&ara->dev, setup->irq, smbalert_irq,
  135. 0, "smbus_alert", alert);
  136. if (res)
  137. return res;
  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 and memory resources are managed so they are 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. return 0;
  150. }
  151. static const struct i2c_device_id smbalert_ids[] = {
  152. { "smbus_alert", 0 },
  153. { /* LIST END */ }
  154. };
  155. MODULE_DEVICE_TABLE(i2c, smbalert_ids);
  156. static struct i2c_driver smbalert_driver = {
  157. .driver = {
  158. .name = "smbus_alert",
  159. },
  160. .probe = smbalert_probe,
  161. .remove = smbalert_remove,
  162. .id_table = smbalert_ids,
  163. };
  164. /**
  165. * i2c_setup_smbus_alert - Setup SMBus alert support
  166. * @adapter: the target adapter
  167. * @setup: setup data for the SMBus alert handler
  168. * Context: can sleep
  169. *
  170. * Setup handling of the SMBus alert protocol on a given I2C bus segment.
  171. *
  172. * Handling can be done either through our IRQ handler, or by the
  173. * adapter (from its handler, periodic polling, or whatever).
  174. *
  175. * NOTE that if we manage the IRQ, we *MUST* know if it's level or
  176. * edge triggered in order to hand it to the workqueue correctly.
  177. * If triggering the alert seems to wedge the system, you probably
  178. * should have said it's level triggered.
  179. *
  180. * This returns the ara client, which should be saved for later use with
  181. * i2c_handle_smbus_alert() and ultimately i2c_unregister_device(); or NULL
  182. * to indicate an error.
  183. */
  184. struct i2c_client *i2c_setup_smbus_alert(struct i2c_adapter *adapter,
  185. struct i2c_smbus_alert_setup *setup)
  186. {
  187. struct i2c_board_info ara_board_info = {
  188. I2C_BOARD_INFO("smbus_alert", 0x0c),
  189. .platform_data = setup,
  190. };
  191. return i2c_new_device(adapter, &ara_board_info);
  192. }
  193. EXPORT_SYMBOL_GPL(i2c_setup_smbus_alert);
  194. /**
  195. * i2c_handle_smbus_alert - Handle an SMBus alert
  196. * @ara: the ARA client on the relevant adapter
  197. * Context: can't sleep
  198. *
  199. * Helper function to be called from an I2C bus driver's interrupt
  200. * handler. It will schedule the alert work, in turn calling the
  201. * corresponding I2C device driver's alert function.
  202. *
  203. * It is assumed that ara is a valid i2c client previously returned by
  204. * i2c_setup_smbus_alert().
  205. */
  206. int i2c_handle_smbus_alert(struct i2c_client *ara)
  207. {
  208. struct i2c_smbus_alert *alert = i2c_get_clientdata(ara);
  209. return schedule_work(&alert->alert);
  210. }
  211. EXPORT_SYMBOL_GPL(i2c_handle_smbus_alert);
  212. module_i2c_driver(smbalert_driver);
  213. MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
  214. MODULE_DESCRIPTION("SMBus protocol extensions support");
  215. MODULE_LICENSE("GPL");