i2c-smbus.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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-2019 Jean Delvare <[email protected]>
  7. */
  8. #include <linux/device.h>
  9. #include <linux/dmi.h>
  10. #include <linux/i2c.h>
  11. #include <linux/i2c-smbus.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/property.h>
  16. #include <linux/slab.h>
  17. #include <linux/workqueue.h>
  18. struct i2c_smbus_alert {
  19. struct work_struct alert;
  20. struct i2c_client *ara; /* Alert response address */
  21. };
  22. struct alert_data {
  23. unsigned short addr;
  24. enum i2c_alert_protocol type;
  25. unsigned int data;
  26. };
  27. /* If this is the alerting device, notify its driver */
  28. static int smbus_do_alert(struct device *dev, void *addrp)
  29. {
  30. struct i2c_client *client = i2c_verify_client(dev);
  31. struct alert_data *data = addrp;
  32. struct i2c_driver *driver;
  33. if (!client || client->addr != data->addr)
  34. return 0;
  35. if (client->flags & I2C_CLIENT_TEN)
  36. return 0;
  37. /*
  38. * Drivers should either disable alerts, or provide at least
  39. * a minimal handler. Lock so the driver won't change.
  40. */
  41. device_lock(dev);
  42. if (client->dev.driver) {
  43. driver = to_i2c_driver(client->dev.driver);
  44. if (driver->alert)
  45. driver->alert(client, data->type, data->data);
  46. else
  47. dev_warn(&client->dev, "no driver alert()!\n");
  48. } else
  49. dev_dbg(&client->dev, "alert with no driver\n");
  50. device_unlock(dev);
  51. /* Stop iterating after we find the device */
  52. return -EBUSY;
  53. }
  54. /*
  55. * The alert IRQ handler needs to hand work off to a task which can issue
  56. * SMBus calls, because those sleeping calls can't be made in IRQ context.
  57. */
  58. static irqreturn_t smbus_alert(int irq, void *d)
  59. {
  60. struct i2c_smbus_alert *alert = d;
  61. struct i2c_client *ara;
  62. ara = alert->ara;
  63. for (;;) {
  64. s32 status;
  65. struct alert_data data;
  66. /*
  67. * Devices with pending alerts reply in address order, low
  68. * to high, because of slave transmit arbitration. After
  69. * responding, an SMBus device stops asserting SMBALERT#.
  70. *
  71. * Note that SMBus 2.0 reserves 10-bit addresses for future
  72. * use. We neither handle them, nor try to use PEC here.
  73. */
  74. status = i2c_smbus_read_byte(ara);
  75. if (status < 0)
  76. break;
  77. data.data = status & 1;
  78. data.addr = status >> 1;
  79. data.type = I2C_PROTOCOL_SMBUS_ALERT;
  80. dev_dbg(&ara->dev, "SMBALERT# from dev 0x%02x, flag %d\n",
  81. data.addr, data.data);
  82. /* Notify driver for the device which issued the alert */
  83. device_for_each_child(&ara->adapter->dev, &data,
  84. smbus_do_alert);
  85. }
  86. return IRQ_HANDLED;
  87. }
  88. static void smbalert_work(struct work_struct *work)
  89. {
  90. struct i2c_smbus_alert *alert;
  91. alert = container_of(work, struct i2c_smbus_alert, alert);
  92. smbus_alert(0, alert);
  93. }
  94. /* Setup SMBALERT# infrastructure */
  95. static int smbalert_probe(struct i2c_client *ara,
  96. const struct i2c_device_id *id)
  97. {
  98. struct i2c_smbus_alert_setup *setup = dev_get_platdata(&ara->dev);
  99. struct i2c_smbus_alert *alert;
  100. struct i2c_adapter *adapter = ara->adapter;
  101. int res, irq;
  102. alert = devm_kzalloc(&ara->dev, sizeof(struct i2c_smbus_alert),
  103. GFP_KERNEL);
  104. if (!alert)
  105. return -ENOMEM;
  106. if (setup) {
  107. irq = setup->irq;
  108. } else {
  109. irq = fwnode_irq_get_byname(dev_fwnode(adapter->dev.parent),
  110. "smbus_alert");
  111. if (irq <= 0)
  112. return irq;
  113. }
  114. INIT_WORK(&alert->alert, smbalert_work);
  115. alert->ara = ara;
  116. if (irq > 0) {
  117. res = devm_request_threaded_irq(&ara->dev, irq,
  118. NULL, smbus_alert,
  119. IRQF_SHARED | IRQF_ONESHOT,
  120. "smbus_alert", alert);
  121. if (res)
  122. return res;
  123. }
  124. i2c_set_clientdata(ara, alert);
  125. dev_info(&adapter->dev, "supports SMBALERT#\n");
  126. return 0;
  127. }
  128. /* IRQ and memory resources are managed so they are freed automatically */
  129. static void smbalert_remove(struct i2c_client *ara)
  130. {
  131. struct i2c_smbus_alert *alert = i2c_get_clientdata(ara);
  132. cancel_work_sync(&alert->alert);
  133. }
  134. static const struct i2c_device_id smbalert_ids[] = {
  135. { "smbus_alert", 0 },
  136. { /* LIST END */ }
  137. };
  138. MODULE_DEVICE_TABLE(i2c, smbalert_ids);
  139. static struct i2c_driver smbalert_driver = {
  140. .driver = {
  141. .name = "smbus_alert",
  142. },
  143. .probe = smbalert_probe,
  144. .remove = smbalert_remove,
  145. .id_table = smbalert_ids,
  146. };
  147. /**
  148. * i2c_handle_smbus_alert - Handle an SMBus alert
  149. * @ara: the ARA client on the relevant adapter
  150. * Context: can't sleep
  151. *
  152. * Helper function to be called from an I2C bus driver's interrupt
  153. * handler. It will schedule the alert work, in turn calling the
  154. * corresponding I2C device driver's alert function.
  155. *
  156. * It is assumed that ara is a valid i2c client previously returned by
  157. * i2c_new_smbus_alert_device().
  158. */
  159. int i2c_handle_smbus_alert(struct i2c_client *ara)
  160. {
  161. struct i2c_smbus_alert *alert = i2c_get_clientdata(ara);
  162. return schedule_work(&alert->alert);
  163. }
  164. EXPORT_SYMBOL_GPL(i2c_handle_smbus_alert);
  165. module_i2c_driver(smbalert_driver);
  166. #if IS_ENABLED(CONFIG_I2C_SLAVE)
  167. #define SMBUS_HOST_NOTIFY_LEN 3
  168. struct i2c_slave_host_notify_status {
  169. u8 index;
  170. u8 addr;
  171. };
  172. static int i2c_slave_host_notify_cb(struct i2c_client *client,
  173. enum i2c_slave_event event, u8 *val)
  174. {
  175. struct i2c_slave_host_notify_status *status = client->dev.platform_data;
  176. switch (event) {
  177. case I2C_SLAVE_WRITE_RECEIVED:
  178. /* We only retrieve the first byte received (addr)
  179. * since there is currently no support to retrieve the data
  180. * parameter from the client.
  181. */
  182. if (status->index == 0)
  183. status->addr = *val;
  184. if (status->index < U8_MAX)
  185. status->index++;
  186. break;
  187. case I2C_SLAVE_STOP:
  188. if (status->index == SMBUS_HOST_NOTIFY_LEN)
  189. i2c_handle_smbus_host_notify(client->adapter,
  190. status->addr);
  191. fallthrough;
  192. case I2C_SLAVE_WRITE_REQUESTED:
  193. status->index = 0;
  194. break;
  195. case I2C_SLAVE_READ_REQUESTED:
  196. case I2C_SLAVE_READ_PROCESSED:
  197. *val = 0xff;
  198. break;
  199. }
  200. return 0;
  201. }
  202. /**
  203. * i2c_new_slave_host_notify_device - get a client for SMBus host-notify support
  204. * @adapter: the target adapter
  205. * Context: can sleep
  206. *
  207. * Setup handling of the SMBus host-notify protocol on a given I2C bus segment.
  208. *
  209. * Handling is done by creating a device and its callback and handling data
  210. * received via the SMBus host-notify address (0x8)
  211. *
  212. * This returns the client, which should be ultimately freed using
  213. * i2c_free_slave_host_notify_device(); or an ERRPTR to indicate an error.
  214. */
  215. struct i2c_client *i2c_new_slave_host_notify_device(struct i2c_adapter *adapter)
  216. {
  217. struct i2c_board_info host_notify_board_info = {
  218. I2C_BOARD_INFO("smbus_host_notify", 0x08),
  219. .flags = I2C_CLIENT_SLAVE,
  220. };
  221. struct i2c_slave_host_notify_status *status;
  222. struct i2c_client *client;
  223. int ret;
  224. status = kzalloc(sizeof(struct i2c_slave_host_notify_status),
  225. GFP_KERNEL);
  226. if (!status)
  227. return ERR_PTR(-ENOMEM);
  228. host_notify_board_info.platform_data = status;
  229. client = i2c_new_client_device(adapter, &host_notify_board_info);
  230. if (IS_ERR(client)) {
  231. kfree(status);
  232. return client;
  233. }
  234. ret = i2c_slave_register(client, i2c_slave_host_notify_cb);
  235. if (ret) {
  236. i2c_unregister_device(client);
  237. kfree(status);
  238. return ERR_PTR(ret);
  239. }
  240. return client;
  241. }
  242. EXPORT_SYMBOL_GPL(i2c_new_slave_host_notify_device);
  243. /**
  244. * i2c_free_slave_host_notify_device - free the client for SMBus host-notify
  245. * support
  246. * @client: the client to free
  247. * Context: can sleep
  248. *
  249. * Free the i2c_client allocated via i2c_new_slave_host_notify_device
  250. */
  251. void i2c_free_slave_host_notify_device(struct i2c_client *client)
  252. {
  253. if (IS_ERR_OR_NULL(client))
  254. return;
  255. i2c_slave_unregister(client);
  256. kfree(client->dev.platform_data);
  257. i2c_unregister_device(client);
  258. }
  259. EXPORT_SYMBOL_GPL(i2c_free_slave_host_notify_device);
  260. #endif
  261. /*
  262. * SPD is not part of SMBus but we include it here for convenience as the
  263. * target systems are the same.
  264. * Restrictions to automatic SPD instantiation:
  265. * - Only works if all filled slots have the same memory type
  266. * - Only works for DDR2, DDR3 and DDR4 for now
  267. * - Only works on systems with 1 to 4 memory slots
  268. */
  269. #if IS_ENABLED(CONFIG_DMI)
  270. void i2c_register_spd(struct i2c_adapter *adap)
  271. {
  272. int n, slot_count = 0, dimm_count = 0;
  273. u16 handle;
  274. u8 common_mem_type = 0x0, mem_type;
  275. u64 mem_size;
  276. const char *name;
  277. while ((handle = dmi_memdev_handle(slot_count)) != 0xffff) {
  278. slot_count++;
  279. /* Skip empty slots */
  280. mem_size = dmi_memdev_size(handle);
  281. if (!mem_size)
  282. continue;
  283. /* Skip undefined memory type */
  284. mem_type = dmi_memdev_type(handle);
  285. if (mem_type <= 0x02) /* Invalid, Other, Unknown */
  286. continue;
  287. if (!common_mem_type) {
  288. /* First filled slot */
  289. common_mem_type = mem_type;
  290. } else {
  291. /* Check that all filled slots have the same type */
  292. if (mem_type != common_mem_type) {
  293. dev_warn(&adap->dev,
  294. "Different memory types mixed, not instantiating SPD\n");
  295. return;
  296. }
  297. }
  298. dimm_count++;
  299. }
  300. /* No useful DMI data, bail out */
  301. if (!dimm_count)
  302. return;
  303. dev_info(&adap->dev, "%d/%d memory slots populated (from DMI)\n",
  304. dimm_count, slot_count);
  305. if (slot_count > 4) {
  306. dev_warn(&adap->dev,
  307. "Systems with more than 4 memory slots not supported yet, not instantiating SPD\n");
  308. return;
  309. }
  310. switch (common_mem_type) {
  311. case 0x13: /* DDR2 */
  312. case 0x18: /* DDR3 */
  313. case 0x1C: /* LPDDR2 */
  314. case 0x1D: /* LPDDR3 */
  315. name = "spd";
  316. break;
  317. case 0x1A: /* DDR4 */
  318. case 0x1E: /* LPDDR4 */
  319. name = "ee1004";
  320. break;
  321. default:
  322. dev_info(&adap->dev,
  323. "Memory type 0x%02x not supported yet, not instantiating SPD\n",
  324. common_mem_type);
  325. return;
  326. }
  327. /*
  328. * We don't know in which slots the memory modules are. We could
  329. * try to guess from the slot names, but that would be rather complex
  330. * and unreliable, so better probe all possible addresses until we
  331. * have found all memory modules.
  332. */
  333. for (n = 0; n < slot_count && dimm_count; n++) {
  334. struct i2c_board_info info;
  335. unsigned short addr_list[2];
  336. memset(&info, 0, sizeof(struct i2c_board_info));
  337. strscpy(info.type, name, I2C_NAME_SIZE);
  338. addr_list[0] = 0x50 + n;
  339. addr_list[1] = I2C_CLIENT_END;
  340. if (!IS_ERR(i2c_new_scanned_device(adap, &info, addr_list, NULL))) {
  341. dev_info(&adap->dev,
  342. "Successfully instantiated SPD at 0x%hx\n",
  343. addr_list[0]);
  344. dimm_count--;
  345. }
  346. }
  347. }
  348. EXPORT_SYMBOL_GPL(i2c_register_spd);
  349. #endif
  350. MODULE_AUTHOR("Jean Delvare <[email protected]>");
  351. MODULE_DESCRIPTION("SMBus protocol extensions support");
  352. MODULE_LICENSE("GPL");