i2c-smbus.c 12 KB

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