/drivers/i2c/i2c-smbus.c

http://github.com/mirrors/linux · C · 201 lines · 127 code · 34 blank · 40 comment · 14 complexity · 2a80f1b972a10491302f710e13e3f472 MD5 · raw file

  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * i2c-smbus.c - SMBus extensions to the I2C protocol
  4. *
  5. * Copyright (C) 2008 David Brownell
  6. * Copyright (C) 2010 Jean Delvare <jdelvare@suse.de>
  7. */
  8. #include <linux/device.h>
  9. #include <linux/i2c.h>
  10. #include <linux/i2c-smbus.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/of_irq.h>
  15. #include <linux/slab.h>
  16. #include <linux/workqueue.h>
  17. struct i2c_smbus_alert {
  18. struct work_struct alert;
  19. struct i2c_client *ara; /* Alert response address */
  20. };
  21. struct alert_data {
  22. unsigned short addr;
  23. enum i2c_alert_protocol type;
  24. unsigned int data;
  25. };
  26. /* If this is the alerting device, notify its driver */
  27. static int smbus_do_alert(struct device *dev, void *addrp)
  28. {
  29. struct i2c_client *client = i2c_verify_client(dev);
  30. struct alert_data *data = addrp;
  31. struct i2c_driver *driver;
  32. if (!client || client->addr != data->addr)
  33. return 0;
  34. if (client->flags & I2C_CLIENT_TEN)
  35. return 0;
  36. /*
  37. * Drivers should either disable alerts, or provide at least
  38. * a minimal handler. Lock so the driver won't change.
  39. */
  40. device_lock(dev);
  41. if (client->dev.driver) {
  42. driver = to_i2c_driver(client->dev.driver);
  43. if (driver->alert)
  44. driver->alert(client, data->type, data->data);
  45. else
  46. dev_warn(&client->dev, "no driver alert()!\n");
  47. } else
  48. dev_dbg(&client->dev, "alert with no driver\n");
  49. device_unlock(dev);
  50. /* Stop iterating after we find the device */
  51. return -EBUSY;
  52. }
  53. /*
  54. * The alert IRQ handler needs to hand work off to a task which can issue
  55. * SMBus calls, because those sleeping calls can't be made in IRQ context.
  56. */
  57. static irqreturn_t smbus_alert(int irq, void *d)
  58. {
  59. struct i2c_smbus_alert *alert = d;
  60. struct i2c_client *ara;
  61. ara = alert->ara;
  62. for (;;) {
  63. s32 status;
  64. struct alert_data data;
  65. /*
  66. * Devices with pending alerts reply in address order, low
  67. * to high, because of slave transmit arbitration. After
  68. * responding, an SMBus device stops asserting SMBALERT#.
  69. *
  70. * Note that SMBus 2.0 reserves 10-bit addresses for future
  71. * use. We neither handle them, nor try to use PEC here.
  72. */
  73. status = i2c_smbus_read_byte(ara);
  74. if (status < 0)
  75. break;
  76. data.data = status & 1;
  77. data.addr = status >> 1;
  78. data.type = I2C_PROTOCOL_SMBUS_ALERT;
  79. dev_dbg(&ara->dev, "SMBALERT# from dev 0x%02x, flag %d\n",
  80. data.addr, data.data);
  81. /* Notify driver for the device which issued the alert */
  82. device_for_each_child(&ara->adapter->dev, &data,
  83. smbus_do_alert);
  84. }
  85. return IRQ_HANDLED;
  86. }
  87. static void smbalert_work(struct work_struct *work)
  88. {
  89. struct i2c_smbus_alert *alert;
  90. alert = container_of(work, struct i2c_smbus_alert, alert);
  91. smbus_alert(0, alert);
  92. }
  93. /* Setup SMBALERT# infrastructure */
  94. static int smbalert_probe(struct i2c_client *ara,
  95. const struct i2c_device_id *id)
  96. {
  97. struct i2c_smbus_alert_setup *setup = dev_get_platdata(&ara->dev);
  98. struct i2c_smbus_alert *alert;
  99. struct i2c_adapter *adapter = ara->adapter;
  100. int res, irq;
  101. alert = devm_kzalloc(&ara->dev, sizeof(struct i2c_smbus_alert),
  102. GFP_KERNEL);
  103. if (!alert)
  104. return -ENOMEM;
  105. if (setup) {
  106. irq = setup->irq;
  107. } else {
  108. irq = of_irq_get_byname(adapter->dev.of_node, "smbus_alert");
  109. if (irq <= 0)
  110. return irq;
  111. }
  112. INIT_WORK(&alert->alert, smbalert_work);
  113. alert->ara = ara;
  114. if (irq > 0) {
  115. res = devm_request_threaded_irq(&ara->dev, irq,
  116. NULL, smbus_alert,
  117. IRQF_SHARED | IRQF_ONESHOT,
  118. "smbus_alert", alert);
  119. if (res)
  120. return res;
  121. }
  122. i2c_set_clientdata(ara, alert);
  123. dev_info(&adapter->dev, "supports SMBALERT#\n");
  124. return 0;
  125. }
  126. /* IRQ and memory resources are managed so they are freed automatically */
  127. static int smbalert_remove(struct i2c_client *ara)
  128. {
  129. struct i2c_smbus_alert *alert = i2c_get_clientdata(ara);
  130. cancel_work_sync(&alert->alert);
  131. return 0;
  132. }
  133. static const struct i2c_device_id smbalert_ids[] = {
  134. { "smbus_alert", 0 },
  135. { /* LIST END */ }
  136. };
  137. MODULE_DEVICE_TABLE(i2c, smbalert_ids);
  138. static struct i2c_driver smbalert_driver = {
  139. .driver = {
  140. .name = "smbus_alert",
  141. },
  142. .probe = smbalert_probe,
  143. .remove = smbalert_remove,
  144. .id_table = smbalert_ids,
  145. };
  146. /**
  147. * i2c_handle_smbus_alert - Handle an SMBus alert
  148. * @ara: the ARA client on the relevant adapter
  149. * Context: can't sleep
  150. *
  151. * Helper function to be called from an I2C bus driver's interrupt
  152. * handler. It will schedule the alert work, in turn calling the
  153. * corresponding I2C device driver's alert function.
  154. *
  155. * It is assumed that ara is a valid i2c client previously returned by
  156. * i2c_new_smbus_alert_device().
  157. */
  158. int i2c_handle_smbus_alert(struct i2c_client *ara)
  159. {
  160. struct i2c_smbus_alert *alert = i2c_get_clientdata(ara);
  161. return schedule_work(&alert->alert);
  162. }
  163. EXPORT_SYMBOL_GPL(i2c_handle_smbus_alert);
  164. module_i2c_driver(smbalert_driver);
  165. MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
  166. MODULE_DESCRIPTION("SMBus protocol extensions support");
  167. MODULE_LICENSE("GPL");