i2c-smbus.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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/device.h>
  18. #include <linux/i2c.h>
  19. #include <linux/i2c-smbus.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/slab.h>
  24. #include <linux/workqueue.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. enum i2c_alert_protocol type;
  34. unsigned int data;
  35. };
  36. /* If this is the alerting device, notify its driver */
  37. static int smbus_do_alert(struct device *dev, void *addrp)
  38. {
  39. struct i2c_client *client = i2c_verify_client(dev);
  40. struct alert_data *data = addrp;
  41. struct i2c_driver *driver;
  42. if (!client || client->addr != data->addr)
  43. return 0;
  44. if (client->flags & I2C_CLIENT_TEN)
  45. return 0;
  46. /*
  47. * Drivers should either disable alerts, or provide at least
  48. * a minimal handler. Lock so the driver won't change.
  49. */
  50. device_lock(dev);
  51. if (client->dev.driver) {
  52. driver = to_i2c_driver(client->dev.driver);
  53. if (driver->alert)
  54. driver->alert(client, data->type, data->data);
  55. else
  56. dev_warn(&client->dev, "no driver alert()!\n");
  57. } else
  58. dev_dbg(&client->dev, "alert with no driver\n");
  59. device_unlock(dev);
  60. /* Stop iterating after we find the device */
  61. return -EBUSY;
  62. }
  63. /*
  64. * The alert IRQ handler needs to hand work off to a task which can issue
  65. * SMBus calls, because those sleeping calls can't be made in IRQ context.
  66. */
  67. static void smbus_alert(struct work_struct *work)
  68. {
  69. struct i2c_smbus_alert *alert;
  70. struct i2c_client *ara;
  71. unsigned short prev_addr = 0; /* Not a valid address */
  72. alert = container_of(work, struct i2c_smbus_alert, alert);
  73. ara = alert->ara;
  74. for (;;) {
  75. s32 status;
  76. struct alert_data data;
  77. /*
  78. * Devices with pending alerts reply in address order, low
  79. * to high, because of slave transmit arbitration. After
  80. * responding, an SMBus device stops asserting SMBALERT#.
  81. *
  82. * Note that SMBus 2.0 reserves 10-bit addresses for future
  83. * use. We neither handle them, nor try to use PEC here.
  84. */
  85. status = i2c_smbus_read_byte(ara);
  86. if (status < 0)
  87. break;
  88. data.data = status & 1;
  89. data.addr = status >> 1;
  90. data.type = I2C_PROTOCOL_SMBUS_ALERT;
  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.data);
  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 = dev_get_platdata(&ara->dev);
  121. struct i2c_smbus_alert *alert;
  122. struct i2c_adapter *adapter = ara->adapter;
  123. int res;
  124. alert = devm_kzalloc(&ara->dev, sizeof(struct i2c_smbus_alert),
  125. 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. return res;
  137. }
  138. i2c_set_clientdata(ara, alert);
  139. dev_info(&adapter->dev, "supports SMBALERT#, %s trigger\n",
  140. setup->alert_edge_triggered ? "edge" : "level");
  141. return 0;
  142. }
  143. /* IRQ and memory resources are managed so they are freed automatically */
  144. static int smbalert_remove(struct i2c_client *ara)
  145. {
  146. struct i2c_smbus_alert *alert = i2c_get_clientdata(ara);
  147. cancel_work_sync(&alert->alert);
  148. return 0;
  149. }
  150. static const struct i2c_device_id smbalert_ids[] = {
  151. { "smbus_alert", 0 },
  152. { /* LIST END */ }
  153. };
  154. MODULE_DEVICE_TABLE(i2c, smbalert_ids);
  155. static struct i2c_driver smbalert_driver = {
  156. .driver = {
  157. .name = "smbus_alert",
  158. },
  159. .probe = smbalert_probe,
  160. .remove = smbalert_remove,
  161. .id_table = smbalert_ids,
  162. };
  163. /**
  164. * i2c_setup_smbus_alert - Setup SMBus alert support
  165. * @adapter: the target adapter
  166. * @setup: setup data for the SMBus alert handler
  167. * Context: can sleep
  168. *
  169. * Setup handling of the SMBus alert protocol on a given I2C bus segment.
  170. *
  171. * Handling can be done either through our IRQ handler, or by the
  172. * adapter (from its handler, periodic polling, or whatever).
  173. *
  174. * NOTE that if we manage the IRQ, we *MUST* know if it's level or
  175. * edge triggered in order to hand it to the workqueue correctly.
  176. * If triggering the alert seems to wedge the system, you probably
  177. * should have said it's level triggered.
  178. *
  179. * This returns the ara client, which should be saved for later use with
  180. * i2c_handle_smbus_alert() and ultimately i2c_unregister_device(); or NULL
  181. * to indicate an error.
  182. */
  183. struct i2c_client *i2c_setup_smbus_alert(struct i2c_adapter *adapter,
  184. struct i2c_smbus_alert_setup *setup)
  185. {
  186. struct i2c_board_info ara_board_info = {
  187. I2C_BOARD_INFO("smbus_alert", 0x0c),
  188. .platform_data = setup,
  189. };
  190. return i2c_new_device(adapter, &ara_board_info);
  191. }
  192. EXPORT_SYMBOL_GPL(i2c_setup_smbus_alert);
  193. /**
  194. * i2c_handle_smbus_alert - Handle an SMBus alert
  195. * @ara: the ARA client on the relevant adapter
  196. * Context: can't sleep
  197. *
  198. * Helper function to be called from an I2C bus driver's interrupt
  199. * handler. It will schedule the alert work, in turn calling the
  200. * corresponding I2C device driver's alert function.
  201. *
  202. * It is assumed that ara is a valid i2c client previously returned by
  203. * i2c_setup_smbus_alert().
  204. */
  205. int i2c_handle_smbus_alert(struct i2c_client *ara)
  206. {
  207. struct i2c_smbus_alert *alert = i2c_get_clientdata(ara);
  208. return schedule_work(&alert->alert);
  209. }
  210. EXPORT_SYMBOL_GPL(i2c_handle_smbus_alert);
  211. static void smbus_host_notify_work(struct work_struct *work)
  212. {
  213. struct alert_data alert;
  214. struct i2c_adapter *adapter;
  215. unsigned long flags;
  216. u16 payload;
  217. u8 addr;
  218. struct smbus_host_notify *data;
  219. data = container_of(work, struct smbus_host_notify, work);
  220. spin_lock_irqsave(&data->lock, flags);
  221. payload = data->payload;
  222. addr = data->addr;
  223. adapter = data->adapter;
  224. /* clear the pending bit and release the spinlock */
  225. data->pending = false;
  226. spin_unlock_irqrestore(&data->lock, flags);
  227. if (!adapter || !addr)
  228. return;
  229. alert.type = I2C_PROTOCOL_SMBUS_HOST_NOTIFY;
  230. alert.addr = addr;
  231. alert.data = payload;
  232. device_for_each_child(&adapter->dev, &alert, smbus_do_alert);
  233. }
  234. /**
  235. * i2c_setup_smbus_host_notify - Allocate a new smbus_host_notify for the given
  236. * I2C adapter.
  237. * @adapter: the adapter we want to associate a Host Notify function
  238. *
  239. * Returns a struct smbus_host_notify pointer on success, and NULL on failure.
  240. * The resulting smbus_host_notify must not be freed afterwards, it is a
  241. * managed resource already.
  242. */
  243. struct smbus_host_notify *i2c_setup_smbus_host_notify(struct i2c_adapter *adap)
  244. {
  245. struct smbus_host_notify *host_notify;
  246. host_notify = devm_kzalloc(&adap->dev, sizeof(struct smbus_host_notify),
  247. GFP_KERNEL);
  248. if (!host_notify)
  249. return NULL;
  250. host_notify->adapter = adap;
  251. spin_lock_init(&host_notify->lock);
  252. INIT_WORK(&host_notify->work, smbus_host_notify_work);
  253. return host_notify;
  254. }
  255. EXPORT_SYMBOL_GPL(i2c_setup_smbus_host_notify);
  256. /**
  257. * i2c_handle_smbus_host_notify - Forward a Host Notify event to the correct
  258. * I2C client.
  259. * @host_notify: the struct host_notify attached to the relevant adapter
  260. * @addr: the I2C address of the notifying device
  261. * @data: the payload of the notification
  262. * Context: can't sleep
  263. *
  264. * Helper function to be called from an I2C bus driver's interrupt
  265. * handler. It will schedule the Host Notify work, in turn calling the
  266. * corresponding I2C device driver's alert function.
  267. *
  268. * host_notify should be a valid pointer previously returned by
  269. * i2c_setup_smbus_host_notify().
  270. */
  271. int i2c_handle_smbus_host_notify(struct smbus_host_notify *host_notify,
  272. unsigned short addr, unsigned int data)
  273. {
  274. unsigned long flags;
  275. struct i2c_adapter *adapter;
  276. if (!host_notify || !host_notify->adapter)
  277. return -EINVAL;
  278. adapter = host_notify->adapter;
  279. spin_lock_irqsave(&host_notify->lock, flags);
  280. if (host_notify->pending) {
  281. spin_unlock_irqrestore(&host_notify->lock, flags);
  282. dev_warn(&adapter->dev, "Host Notify already scheduled.\n");
  283. return -EBUSY;
  284. }
  285. host_notify->payload = data;
  286. host_notify->addr = addr;
  287. /* Mark that there is a pending notification and release the lock */
  288. host_notify->pending = true;
  289. spin_unlock_irqrestore(&host_notify->lock, flags);
  290. return schedule_work(&host_notify->work);
  291. }
  292. EXPORT_SYMBOL_GPL(i2c_handle_smbus_host_notify);
  293. module_i2c_driver(smbalert_driver);
  294. MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
  295. MODULE_DESCRIPTION("SMBus protocol extensions support");
  296. MODULE_LICENSE("GPL");