/drivers/i2c/i2c-core.c

http://github.com/mirrors/linux · C · 3409 lines · 2286 code · 523 blank · 600 comment · 439 complexity · d25163411f21d600ab4a7d548aa78732 MD5 · raw file

Large files are truncated click here to view the full file

  1. /* i2c-core.c - a device driver for the iic-bus interface */
  2. /* ------------------------------------------------------------------------- */
  3. /* Copyright (C) 1995-99 Simon G. Vogl
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details. */
  12. /* ------------------------------------------------------------------------- */
  13. /* With some changes from KyÜsti Mälkki <kmalkki@cc.hut.fi>.
  14. All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
  15. SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
  16. Jean Delvare <jdelvare@suse.de>
  17. Mux support by Rodolfo Giometti <giometti@enneenne.com> and
  18. Michael Lawnick <michael.lawnick.ext@nsn.com>
  19. OF support is copyright (c) 2008 Jochen Friedrich <jochen@scram.de>
  20. (based on a previous patch from Jon Smirl <jonsmirl@gmail.com>) and
  21. (c) 2013 Wolfram Sang <wsa@the-dreams.de>
  22. I2C ACPI code Copyright (C) 2014 Intel Corp
  23. Author: Lan Tianyu <tianyu.lan@intel.com>
  24. I2C slave support (c) 2014 by Wolfram Sang <wsa@sang-engineering.com>
  25. */
  26. #define pr_fmt(fmt) "i2c-core: " fmt
  27. #include <dt-bindings/i2c/i2c.h>
  28. #include <asm/uaccess.h>
  29. #include <linux/acpi.h>
  30. #include <linux/clk/clk-conf.h>
  31. #include <linux/completion.h>
  32. #include <linux/delay.h>
  33. #include <linux/err.h>
  34. #include <linux/errno.h>
  35. #include <linux/gpio.h>
  36. #include <linux/hardirq.h>
  37. #include <linux/i2c.h>
  38. #include <linux/idr.h>
  39. #include <linux/init.h>
  40. #include <linux/irqflags.h>
  41. #include <linux/jump_label.h>
  42. #include <linux/kernel.h>
  43. #include <linux/module.h>
  44. #include <linux/mutex.h>
  45. #include <linux/of_device.h>
  46. #include <linux/of.h>
  47. #include <linux/of_irq.h>
  48. #include <linux/pm_domain.h>
  49. #include <linux/pm_runtime.h>
  50. #include <linux/pm_wakeirq.h>
  51. #include <linux/property.h>
  52. #include <linux/rwsem.h>
  53. #include <linux/slab.h>
  54. #include "i2c-core.h"
  55. #define CREATE_TRACE_POINTS
  56. #include <trace/events/i2c.h>
  57. #define I2C_ADDR_OFFSET_TEN_BIT 0xa000
  58. #define I2C_ADDR_OFFSET_SLAVE 0x1000
  59. /* core_lock protects i2c_adapter_idr, and guarantees
  60. that device detection, deletion of detected devices, and attach_adapter
  61. calls are serialized */
  62. static DEFINE_MUTEX(core_lock);
  63. static DEFINE_IDR(i2c_adapter_idr);
  64. static struct device_type i2c_client_type;
  65. static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
  66. static struct static_key i2c_trace_msg = STATIC_KEY_INIT_FALSE;
  67. static bool is_registered;
  68. void i2c_transfer_trace_reg(void)
  69. {
  70. static_key_slow_inc(&i2c_trace_msg);
  71. }
  72. void i2c_transfer_trace_unreg(void)
  73. {
  74. static_key_slow_dec(&i2c_trace_msg);
  75. }
  76. #if defined(CONFIG_ACPI)
  77. struct acpi_i2c_handler_data {
  78. struct acpi_connection_info info;
  79. struct i2c_adapter *adapter;
  80. };
  81. struct gsb_buffer {
  82. u8 status;
  83. u8 len;
  84. union {
  85. u16 wdata;
  86. u8 bdata;
  87. u8 data[0];
  88. };
  89. } __packed;
  90. struct acpi_i2c_lookup {
  91. struct i2c_board_info *info;
  92. acpi_handle adapter_handle;
  93. acpi_handle device_handle;
  94. };
  95. static int acpi_i2c_fill_info(struct acpi_resource *ares, void *data)
  96. {
  97. struct acpi_i2c_lookup *lookup = data;
  98. struct i2c_board_info *info = lookup->info;
  99. struct acpi_resource_i2c_serialbus *sb;
  100. acpi_status status;
  101. if (info->addr || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
  102. return 1;
  103. sb = &ares->data.i2c_serial_bus;
  104. if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C)
  105. return 1;
  106. status = acpi_get_handle(lookup->device_handle,
  107. sb->resource_source.string_ptr,
  108. &lookup->adapter_handle);
  109. if (!ACPI_SUCCESS(status))
  110. return 1;
  111. info->addr = sb->slave_address;
  112. if (sb->access_mode == ACPI_I2C_10BIT_MODE)
  113. info->flags |= I2C_CLIENT_TEN;
  114. return 1;
  115. }
  116. static int acpi_i2c_get_info(struct acpi_device *adev,
  117. struct i2c_board_info *info,
  118. acpi_handle *adapter_handle)
  119. {
  120. struct list_head resource_list;
  121. struct resource_entry *entry;
  122. struct acpi_i2c_lookup lookup;
  123. int ret;
  124. if (acpi_bus_get_status(adev) || !adev->status.present ||
  125. acpi_device_enumerated(adev))
  126. return -EINVAL;
  127. memset(info, 0, sizeof(*info));
  128. info->fwnode = acpi_fwnode_handle(adev);
  129. memset(&lookup, 0, sizeof(lookup));
  130. lookup.device_handle = acpi_device_handle(adev);
  131. lookup.info = info;
  132. /* Look up for I2cSerialBus resource */
  133. INIT_LIST_HEAD(&resource_list);
  134. ret = acpi_dev_get_resources(adev, &resource_list,
  135. acpi_i2c_fill_info, &lookup);
  136. acpi_dev_free_resource_list(&resource_list);
  137. if (ret < 0 || !info->addr)
  138. return -EINVAL;
  139. *adapter_handle = lookup.adapter_handle;
  140. /* Then fill IRQ number if any */
  141. ret = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
  142. if (ret < 0)
  143. return -EINVAL;
  144. resource_list_for_each_entry(entry, &resource_list) {
  145. if (resource_type(entry->res) == IORESOURCE_IRQ) {
  146. info->irq = entry->res->start;
  147. break;
  148. }
  149. }
  150. acpi_dev_free_resource_list(&resource_list);
  151. strlcpy(info->type, dev_name(&adev->dev), sizeof(info->type));
  152. return 0;
  153. }
  154. static void acpi_i2c_register_device(struct i2c_adapter *adapter,
  155. struct acpi_device *adev,
  156. struct i2c_board_info *info)
  157. {
  158. adev->power.flags.ignore_parent = true;
  159. acpi_device_set_enumerated(adev);
  160. if (!i2c_new_device(adapter, info)) {
  161. adev->power.flags.ignore_parent = false;
  162. dev_err(&adapter->dev,
  163. "failed to add I2C device %s from ACPI\n",
  164. dev_name(&adev->dev));
  165. }
  166. }
  167. static acpi_status acpi_i2c_add_device(acpi_handle handle, u32 level,
  168. void *data, void **return_value)
  169. {
  170. struct i2c_adapter *adapter = data;
  171. struct acpi_device *adev;
  172. acpi_handle adapter_handle;
  173. struct i2c_board_info info;
  174. if (acpi_bus_get_device(handle, &adev))
  175. return AE_OK;
  176. if (acpi_i2c_get_info(adev, &info, &adapter_handle))
  177. return AE_OK;
  178. if (adapter_handle != ACPI_HANDLE(&adapter->dev))
  179. return AE_OK;
  180. acpi_i2c_register_device(adapter, adev, &info);
  181. return AE_OK;
  182. }
  183. #define ACPI_I2C_MAX_SCAN_DEPTH 32
  184. /**
  185. * acpi_i2c_register_devices - enumerate I2C slave devices behind adapter
  186. * @adap: pointer to adapter
  187. *
  188. * Enumerate all I2C slave devices behind this adapter by walking the ACPI
  189. * namespace. When a device is found it will be added to the Linux device
  190. * model and bound to the corresponding ACPI handle.
  191. */
  192. static void acpi_i2c_register_devices(struct i2c_adapter *adap)
  193. {
  194. acpi_status status;
  195. if (!has_acpi_companion(&adap->dev))
  196. return;
  197. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  198. ACPI_I2C_MAX_SCAN_DEPTH,
  199. acpi_i2c_add_device, NULL,
  200. adap, NULL);
  201. if (ACPI_FAILURE(status))
  202. dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
  203. }
  204. static int acpi_i2c_match_adapter(struct device *dev, void *data)
  205. {
  206. struct i2c_adapter *adapter = i2c_verify_adapter(dev);
  207. if (!adapter)
  208. return 0;
  209. return ACPI_HANDLE(dev) == (acpi_handle)data;
  210. }
  211. static int acpi_i2c_match_device(struct device *dev, void *data)
  212. {
  213. return ACPI_COMPANION(dev) == data;
  214. }
  215. static struct i2c_adapter *acpi_i2c_find_adapter_by_handle(acpi_handle handle)
  216. {
  217. struct device *dev;
  218. dev = bus_find_device(&i2c_bus_type, NULL, handle,
  219. acpi_i2c_match_adapter);
  220. return dev ? i2c_verify_adapter(dev) : NULL;
  221. }
  222. static struct i2c_client *acpi_i2c_find_client_by_adev(struct acpi_device *adev)
  223. {
  224. struct device *dev;
  225. dev = bus_find_device(&i2c_bus_type, NULL, adev, acpi_i2c_match_device);
  226. return dev ? i2c_verify_client(dev) : NULL;
  227. }
  228. static int acpi_i2c_notify(struct notifier_block *nb, unsigned long value,
  229. void *arg)
  230. {
  231. struct acpi_device *adev = arg;
  232. struct i2c_board_info info;
  233. acpi_handle adapter_handle;
  234. struct i2c_adapter *adapter;
  235. struct i2c_client *client;
  236. switch (value) {
  237. case ACPI_RECONFIG_DEVICE_ADD:
  238. if (acpi_i2c_get_info(adev, &info, &adapter_handle))
  239. break;
  240. adapter = acpi_i2c_find_adapter_by_handle(adapter_handle);
  241. if (!adapter)
  242. break;
  243. acpi_i2c_register_device(adapter, adev, &info);
  244. break;
  245. case ACPI_RECONFIG_DEVICE_REMOVE:
  246. if (!acpi_device_enumerated(adev))
  247. break;
  248. client = acpi_i2c_find_client_by_adev(adev);
  249. if (!client)
  250. break;
  251. i2c_unregister_device(client);
  252. put_device(&client->dev);
  253. break;
  254. }
  255. return NOTIFY_OK;
  256. }
  257. static struct notifier_block i2c_acpi_notifier = {
  258. .notifier_call = acpi_i2c_notify,
  259. };
  260. #else /* CONFIG_ACPI */
  261. static inline void acpi_i2c_register_devices(struct i2c_adapter *adap) { }
  262. extern struct notifier_block i2c_acpi_notifier;
  263. #endif /* CONFIG_ACPI */
  264. #ifdef CONFIG_ACPI_I2C_OPREGION
  265. static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
  266. u8 cmd, u8 *data, u8 data_len)
  267. {
  268. struct i2c_msg msgs[2];
  269. int ret;
  270. u8 *buffer;
  271. buffer = kzalloc(data_len, GFP_KERNEL);
  272. if (!buffer)
  273. return AE_NO_MEMORY;
  274. msgs[0].addr = client->addr;
  275. msgs[0].flags = client->flags;
  276. msgs[0].len = 1;
  277. msgs[0].buf = &cmd;
  278. msgs[1].addr = client->addr;
  279. msgs[1].flags = client->flags | I2C_M_RD;
  280. msgs[1].len = data_len;
  281. msgs[1].buf = buffer;
  282. ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  283. if (ret < 0)
  284. dev_err(&client->adapter->dev, "i2c read failed\n");
  285. else
  286. memcpy(data, buffer, data_len);
  287. kfree(buffer);
  288. return ret;
  289. }
  290. static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
  291. u8 cmd, u8 *data, u8 data_len)
  292. {
  293. struct i2c_msg msgs[1];
  294. u8 *buffer;
  295. int ret = AE_OK;
  296. buffer = kzalloc(data_len + 1, GFP_KERNEL);
  297. if (!buffer)
  298. return AE_NO_MEMORY;
  299. buffer[0] = cmd;
  300. memcpy(buffer + 1, data, data_len);
  301. msgs[0].addr = client->addr;
  302. msgs[0].flags = client->flags;
  303. msgs[0].len = data_len + 1;
  304. msgs[0].buf = buffer;
  305. ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  306. if (ret < 0)
  307. dev_err(&client->adapter->dev, "i2c write failed\n");
  308. kfree(buffer);
  309. return ret;
  310. }
  311. static acpi_status
  312. acpi_i2c_space_handler(u32 function, acpi_physical_address command,
  313. u32 bits, u64 *value64,
  314. void *handler_context, void *region_context)
  315. {
  316. struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
  317. struct acpi_i2c_handler_data *data = handler_context;
  318. struct acpi_connection_info *info = &data->info;
  319. struct acpi_resource_i2c_serialbus *sb;
  320. struct i2c_adapter *adapter = data->adapter;
  321. struct i2c_client *client;
  322. struct acpi_resource *ares;
  323. u32 accessor_type = function >> 16;
  324. u8 action = function & ACPI_IO_MASK;
  325. acpi_status ret;
  326. int status;
  327. ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
  328. if (ACPI_FAILURE(ret))
  329. return ret;
  330. client = kzalloc(sizeof(*client), GFP_KERNEL);
  331. if (!client) {
  332. ret = AE_NO_MEMORY;
  333. goto err;
  334. }
  335. if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) {
  336. ret = AE_BAD_PARAMETER;
  337. goto err;
  338. }
  339. sb = &ares->data.i2c_serial_bus;
  340. if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
  341. ret = AE_BAD_PARAMETER;
  342. goto err;
  343. }
  344. client->adapter = adapter;
  345. client->addr = sb->slave_address;
  346. if (sb->access_mode == ACPI_I2C_10BIT_MODE)
  347. client->flags |= I2C_CLIENT_TEN;
  348. switch (accessor_type) {
  349. case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV:
  350. if (action == ACPI_READ) {
  351. status = i2c_smbus_read_byte(client);
  352. if (status >= 0) {
  353. gsb->bdata = status;
  354. status = 0;
  355. }
  356. } else {
  357. status = i2c_smbus_write_byte(client, gsb->bdata);
  358. }
  359. break;
  360. case ACPI_GSB_ACCESS_ATTRIB_BYTE:
  361. if (action == ACPI_READ) {
  362. status = i2c_smbus_read_byte_data(client, command);
  363. if (status >= 0) {
  364. gsb->bdata = status;
  365. status = 0;
  366. }
  367. } else {
  368. status = i2c_smbus_write_byte_data(client, command,
  369. gsb->bdata);
  370. }
  371. break;
  372. case ACPI_GSB_ACCESS_ATTRIB_WORD:
  373. if (action == ACPI_READ) {
  374. status = i2c_smbus_read_word_data(client, command);
  375. if (status >= 0) {
  376. gsb->wdata = status;
  377. status = 0;
  378. }
  379. } else {
  380. status = i2c_smbus_write_word_data(client, command,
  381. gsb->wdata);
  382. }
  383. break;
  384. case ACPI_GSB_ACCESS_ATTRIB_BLOCK:
  385. if (action == ACPI_READ) {
  386. status = i2c_smbus_read_block_data(client, command,
  387. gsb->data);
  388. if (status >= 0) {
  389. gsb->len = status;
  390. status = 0;
  391. }
  392. } else {
  393. status = i2c_smbus_write_block_data(client, command,
  394. gsb->len, gsb->data);
  395. }
  396. break;
  397. case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE:
  398. if (action == ACPI_READ) {
  399. status = acpi_gsb_i2c_read_bytes(client, command,
  400. gsb->data, info->access_length);
  401. if (status > 0)
  402. status = 0;
  403. } else {
  404. status = acpi_gsb_i2c_write_bytes(client, command,
  405. gsb->data, info->access_length);
  406. }
  407. break;
  408. default:
  409. dev_warn(&adapter->dev, "protocol 0x%02x not supported for client 0x%02x\n",
  410. accessor_type, client->addr);
  411. ret = AE_BAD_PARAMETER;
  412. goto err;
  413. }
  414. gsb->status = status;
  415. err:
  416. kfree(client);
  417. ACPI_FREE(ares);
  418. return ret;
  419. }
  420. static int acpi_i2c_install_space_handler(struct i2c_adapter *adapter)
  421. {
  422. acpi_handle handle;
  423. struct acpi_i2c_handler_data *data;
  424. acpi_status status;
  425. if (!adapter->dev.parent)
  426. return -ENODEV;
  427. handle = ACPI_HANDLE(adapter->dev.parent);
  428. if (!handle)
  429. return -ENODEV;
  430. data = kzalloc(sizeof(struct acpi_i2c_handler_data),
  431. GFP_KERNEL);
  432. if (!data)
  433. return -ENOMEM;
  434. data->adapter = adapter;
  435. status = acpi_bus_attach_private_data(handle, (void *)data);
  436. if (ACPI_FAILURE(status)) {
  437. kfree(data);
  438. return -ENOMEM;
  439. }
  440. status = acpi_install_address_space_handler(handle,
  441. ACPI_ADR_SPACE_GSBUS,
  442. &acpi_i2c_space_handler,
  443. NULL,
  444. data);
  445. if (ACPI_FAILURE(status)) {
  446. dev_err(&adapter->dev, "Error installing i2c space handler\n");
  447. acpi_bus_detach_private_data(handle);
  448. kfree(data);
  449. return -ENOMEM;
  450. }
  451. acpi_walk_dep_device_list(handle);
  452. return 0;
  453. }
  454. static void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter)
  455. {
  456. acpi_handle handle;
  457. struct acpi_i2c_handler_data *data;
  458. acpi_status status;
  459. if (!adapter->dev.parent)
  460. return;
  461. handle = ACPI_HANDLE(adapter->dev.parent);
  462. if (!handle)
  463. return;
  464. acpi_remove_address_space_handler(handle,
  465. ACPI_ADR_SPACE_GSBUS,
  466. &acpi_i2c_space_handler);
  467. status = acpi_bus_get_private_data(handle, (void **)&data);
  468. if (ACPI_SUCCESS(status))
  469. kfree(data);
  470. acpi_bus_detach_private_data(handle);
  471. }
  472. #else /* CONFIG_ACPI_I2C_OPREGION */
  473. static inline void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter)
  474. { }
  475. static inline int acpi_i2c_install_space_handler(struct i2c_adapter *adapter)
  476. { return 0; }
  477. #endif /* CONFIG_ACPI_I2C_OPREGION */
  478. /* ------------------------------------------------------------------------- */
  479. static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
  480. const struct i2c_client *client)
  481. {
  482. while (id->name[0]) {
  483. if (strcmp(client->name, id->name) == 0)
  484. return id;
  485. id++;
  486. }
  487. return NULL;
  488. }
  489. static int i2c_device_match(struct device *dev, struct device_driver *drv)
  490. {
  491. struct i2c_client *client = i2c_verify_client(dev);
  492. struct i2c_driver *driver;
  493. if (!client)
  494. return 0;
  495. /* Attempt an OF style match */
  496. if (of_driver_match_device(dev, drv))
  497. return 1;
  498. /* Then ACPI style match */
  499. if (acpi_driver_match_device(dev, drv))
  500. return 1;
  501. driver = to_i2c_driver(drv);
  502. /* match on an id table if there is one */
  503. if (driver->id_table)
  504. return i2c_match_id(driver->id_table, client) != NULL;
  505. return 0;
  506. }
  507. static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
  508. {
  509. struct i2c_client *client = to_i2c_client(dev);
  510. int rc;
  511. rc = acpi_device_uevent_modalias(dev, env);
  512. if (rc != -ENODEV)
  513. return rc;
  514. return add_uevent_var(env, "MODALIAS=%s%s", I2C_MODULE_PREFIX, client->name);
  515. }
  516. /* i2c bus recovery routines */
  517. static int get_scl_gpio_value(struct i2c_adapter *adap)
  518. {
  519. return gpio_get_value(adap->bus_recovery_info->scl_gpio);
  520. }
  521. static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
  522. {
  523. gpio_set_value(adap->bus_recovery_info->scl_gpio, val);
  524. }
  525. static int get_sda_gpio_value(struct i2c_adapter *adap)
  526. {
  527. return gpio_get_value(adap->bus_recovery_info->sda_gpio);
  528. }
  529. static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap)
  530. {
  531. struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
  532. struct device *dev = &adap->dev;
  533. int ret = 0;
  534. ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN |
  535. GPIOF_OUT_INIT_HIGH, "i2c-scl");
  536. if (ret) {
  537. dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio);
  538. return ret;
  539. }
  540. if (bri->get_sda) {
  541. if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) {
  542. /* work without SDA polling */
  543. dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n",
  544. bri->sda_gpio);
  545. bri->get_sda = NULL;
  546. }
  547. }
  548. return ret;
  549. }
  550. static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap)
  551. {
  552. struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
  553. if (bri->get_sda)
  554. gpio_free(bri->sda_gpio);
  555. gpio_free(bri->scl_gpio);
  556. }
  557. /*
  558. * We are generating clock pulses. ndelay() determines durating of clk pulses.
  559. * We will generate clock with rate 100 KHz and so duration of both clock levels
  560. * is: delay in ns = (10^6 / 100) / 2
  561. */
  562. #define RECOVERY_NDELAY 5000
  563. #define RECOVERY_CLK_CNT 9
  564. static int i2c_generic_recovery(struct i2c_adapter *adap)
  565. {
  566. struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
  567. int i = 0, val = 1, ret = 0;
  568. if (bri->prepare_recovery)
  569. bri->prepare_recovery(adap);
  570. bri->set_scl(adap, val);
  571. ndelay(RECOVERY_NDELAY);
  572. /*
  573. * By this time SCL is high, as we need to give 9 falling-rising edges
  574. */
  575. while (i++ < RECOVERY_CLK_CNT * 2) {
  576. if (val) {
  577. /* Break if SDA is high */
  578. if (bri->get_sda && bri->get_sda(adap))
  579. break;
  580. /* SCL shouldn't be low here */
  581. if (!bri->get_scl(adap)) {
  582. dev_err(&adap->dev,
  583. "SCL is stuck low, exit recovery\n");
  584. ret = -EBUSY;
  585. break;
  586. }
  587. }
  588. val = !val;
  589. bri->set_scl(adap, val);
  590. ndelay(RECOVERY_NDELAY);
  591. }
  592. if (bri->unprepare_recovery)
  593. bri->unprepare_recovery(adap);
  594. return ret;
  595. }
  596. int i2c_generic_scl_recovery(struct i2c_adapter *adap)
  597. {
  598. return i2c_generic_recovery(adap);
  599. }
  600. EXPORT_SYMBOL_GPL(i2c_generic_scl_recovery);
  601. int i2c_generic_gpio_recovery(struct i2c_adapter *adap)
  602. {
  603. int ret;
  604. ret = i2c_get_gpios_for_recovery(adap);
  605. if (ret)
  606. return ret;
  607. ret = i2c_generic_recovery(adap);
  608. i2c_put_gpios_for_recovery(adap);
  609. return ret;
  610. }
  611. EXPORT_SYMBOL_GPL(i2c_generic_gpio_recovery);
  612. int i2c_recover_bus(struct i2c_adapter *adap)
  613. {
  614. if (!adap->bus_recovery_info)
  615. return -EOPNOTSUPP;
  616. dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
  617. return adap->bus_recovery_info->recover_bus(adap);
  618. }
  619. EXPORT_SYMBOL_GPL(i2c_recover_bus);
  620. static void i2c_init_recovery(struct i2c_adapter *adap)
  621. {
  622. struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
  623. char *err_str;
  624. if (!bri)
  625. return;
  626. if (!bri->recover_bus) {
  627. err_str = "no recover_bus() found";
  628. goto err;
  629. }
  630. /* Generic GPIO recovery */
  631. if (bri->recover_bus == i2c_generic_gpio_recovery) {
  632. if (!gpio_is_valid(bri->scl_gpio)) {
  633. err_str = "invalid SCL gpio";
  634. goto err;
  635. }
  636. if (gpio_is_valid(bri->sda_gpio))
  637. bri->get_sda = get_sda_gpio_value;
  638. else
  639. bri->get_sda = NULL;
  640. bri->get_scl = get_scl_gpio_value;
  641. bri->set_scl = set_scl_gpio_value;
  642. } else if (bri->recover_bus == i2c_generic_scl_recovery) {
  643. /* Generic SCL recovery */
  644. if (!bri->set_scl || !bri->get_scl) {
  645. err_str = "no {get|set}_scl() found";
  646. goto err;
  647. }
  648. }
  649. return;
  650. err:
  651. dev_err(&adap->dev, "Not using recovery: %s\n", err_str);
  652. adap->bus_recovery_info = NULL;
  653. }
  654. static int i2c_device_probe(struct device *dev)
  655. {
  656. struct i2c_client *client = i2c_verify_client(dev);
  657. struct i2c_driver *driver;
  658. int status;
  659. if (!client)
  660. return 0;
  661. if (!client->irq) {
  662. int irq = -ENOENT;
  663. if (dev->of_node) {
  664. irq = of_irq_get_byname(dev->of_node, "irq");
  665. if (irq == -EINVAL || irq == -ENODATA)
  666. irq = of_irq_get(dev->of_node, 0);
  667. } else if (ACPI_COMPANION(dev)) {
  668. irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 0);
  669. }
  670. if (irq == -EPROBE_DEFER)
  671. return irq;
  672. if (irq < 0)
  673. irq = 0;
  674. client->irq = irq;
  675. }
  676. driver = to_i2c_driver(dev->driver);
  677. if (!driver->probe || !driver->id_table)
  678. return -ENODEV;
  679. if (client->flags & I2C_CLIENT_WAKE) {
  680. int wakeirq = -ENOENT;
  681. if (dev->of_node) {
  682. wakeirq = of_irq_get_byname(dev->of_node, "wakeup");
  683. if (wakeirq == -EPROBE_DEFER)
  684. return wakeirq;
  685. }
  686. device_init_wakeup(&client->dev, true);
  687. if (wakeirq > 0 && wakeirq != client->irq)
  688. status = dev_pm_set_dedicated_wake_irq(dev, wakeirq);
  689. else if (client->irq > 0)
  690. status = dev_pm_set_wake_irq(dev, client->irq);
  691. else
  692. status = 0;
  693. if (status)
  694. dev_warn(&client->dev, "failed to set up wakeup irq");
  695. }
  696. dev_dbg(dev, "probe\n");
  697. status = of_clk_set_defaults(dev->of_node, false);
  698. if (status < 0)
  699. goto err_clear_wakeup_irq;
  700. status = dev_pm_domain_attach(&client->dev, true);
  701. if (status == -EPROBE_DEFER)
  702. goto err_clear_wakeup_irq;
  703. status = driver->probe(client, i2c_match_id(driver->id_table, client));
  704. if (status)
  705. goto err_detach_pm_domain;
  706. return 0;
  707. err_detach_pm_domain:
  708. dev_pm_domain_detach(&client->dev, true);
  709. err_clear_wakeup_irq:
  710. dev_pm_clear_wake_irq(&client->dev);
  711. device_init_wakeup(&client->dev, false);
  712. return status;
  713. }
  714. static int i2c_device_remove(struct device *dev)
  715. {
  716. struct i2c_client *client = i2c_verify_client(dev);
  717. struct i2c_driver *driver;
  718. int status = 0;
  719. if (!client || !dev->driver)
  720. return 0;
  721. driver = to_i2c_driver(dev->driver);
  722. if (driver->remove) {
  723. dev_dbg(dev, "remove\n");
  724. status = driver->remove(client);
  725. }
  726. dev_pm_domain_detach(&client->dev, true);
  727. dev_pm_clear_wake_irq(&client->dev);
  728. device_init_wakeup(&client->dev, false);
  729. return status;
  730. }
  731. static void i2c_device_shutdown(struct device *dev)
  732. {
  733. struct i2c_client *client = i2c_verify_client(dev);
  734. struct i2c_driver *driver;
  735. if (!client || !dev->driver)
  736. return;
  737. driver = to_i2c_driver(dev->driver);
  738. if (driver->shutdown)
  739. driver->shutdown(client);
  740. }
  741. static void i2c_client_dev_release(struct device *dev)
  742. {
  743. kfree(to_i2c_client(dev));
  744. }
  745. static ssize_t
  746. show_name(struct device *dev, struct device_attribute *attr, char *buf)
  747. {
  748. return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
  749. to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
  750. }
  751. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  752. static ssize_t
  753. show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
  754. {
  755. struct i2c_client *client = to_i2c_client(dev);
  756. int len;
  757. len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
  758. if (len != -ENODEV)
  759. return len;
  760. return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
  761. }
  762. static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
  763. static struct attribute *i2c_dev_attrs[] = {
  764. &dev_attr_name.attr,
  765. /* modalias helps coldplug: modprobe $(cat .../modalias) */
  766. &dev_attr_modalias.attr,
  767. NULL
  768. };
  769. ATTRIBUTE_GROUPS(i2c_dev);
  770. struct bus_type i2c_bus_type = {
  771. .name = "i2c",
  772. .match = i2c_device_match,
  773. .probe = i2c_device_probe,
  774. .remove = i2c_device_remove,
  775. .shutdown = i2c_device_shutdown,
  776. };
  777. EXPORT_SYMBOL_GPL(i2c_bus_type);
  778. static struct device_type i2c_client_type = {
  779. .groups = i2c_dev_groups,
  780. .uevent = i2c_device_uevent,
  781. .release = i2c_client_dev_release,
  782. };
  783. /**
  784. * i2c_verify_client - return parameter as i2c_client, or NULL
  785. * @dev: device, probably from some driver model iterator
  786. *
  787. * When traversing the driver model tree, perhaps using driver model
  788. * iterators like @device_for_each_child(), you can't assume very much
  789. * about the nodes you find. Use this function to avoid oopses caused
  790. * by wrongly treating some non-I2C device as an i2c_client.
  791. */
  792. struct i2c_client *i2c_verify_client(struct device *dev)
  793. {
  794. return (dev->type == &i2c_client_type)
  795. ? to_i2c_client(dev)
  796. : NULL;
  797. }
  798. EXPORT_SYMBOL(i2c_verify_client);
  799. /* Return a unique address which takes the flags of the client into account */
  800. static unsigned short i2c_encode_flags_to_addr(struct i2c_client *client)
  801. {
  802. unsigned short addr = client->addr;
  803. /* For some client flags, add an arbitrary offset to avoid collisions */
  804. if (client->flags & I2C_CLIENT_TEN)
  805. addr |= I2C_ADDR_OFFSET_TEN_BIT;
  806. if (client->flags & I2C_CLIENT_SLAVE)
  807. addr |= I2C_ADDR_OFFSET_SLAVE;
  808. return addr;
  809. }
  810. /* This is a permissive address validity check, I2C address map constraints
  811. * are purposely not enforced, except for the general call address. */
  812. static int i2c_check_addr_validity(unsigned addr, unsigned short flags)
  813. {
  814. if (flags & I2C_CLIENT_TEN) {
  815. /* 10-bit address, all values are valid */
  816. if (addr > 0x3ff)
  817. return -EINVAL;
  818. } else {
  819. /* 7-bit address, reject the general call address */
  820. if (addr == 0x00 || addr > 0x7f)
  821. return -EINVAL;
  822. }
  823. return 0;
  824. }
  825. /* And this is a strict address validity check, used when probing. If a
  826. * device uses a reserved address, then it shouldn't be probed. 7-bit
  827. * addressing is assumed, 10-bit address devices are rare and should be
  828. * explicitly enumerated. */
  829. static int i2c_check_7bit_addr_validity_strict(unsigned short addr)
  830. {
  831. /*
  832. * Reserved addresses per I2C specification:
  833. * 0x00 General call address / START byte
  834. * 0x01 CBUS address
  835. * 0x02 Reserved for different bus format
  836. * 0x03 Reserved for future purposes
  837. * 0x04-0x07 Hs-mode master code
  838. * 0x78-0x7b 10-bit slave addressing
  839. * 0x7c-0x7f Reserved for future purposes
  840. */
  841. if (addr < 0x08 || addr > 0x77)
  842. return -EINVAL;
  843. return 0;
  844. }
  845. static int __i2c_check_addr_busy(struct device *dev, void *addrp)
  846. {
  847. struct i2c_client *client = i2c_verify_client(dev);
  848. int addr = *(int *)addrp;
  849. if (client && i2c_encode_flags_to_addr(client) == addr)
  850. return -EBUSY;
  851. return 0;
  852. }
  853. /* walk up mux tree */
  854. static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
  855. {
  856. struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
  857. int result;
  858. result = device_for_each_child(&adapter->dev, &addr,
  859. __i2c_check_addr_busy);
  860. if (!result && parent)
  861. result = i2c_check_mux_parents(parent, addr);
  862. return result;
  863. }
  864. /* recurse down mux tree */
  865. static int i2c_check_mux_children(struct device *dev, void *addrp)
  866. {
  867. int result;
  868. if (dev->type == &i2c_adapter_type)
  869. result = device_for_each_child(dev, addrp,
  870. i2c_check_mux_children);
  871. else
  872. result = __i2c_check_addr_busy(dev, addrp);
  873. return result;
  874. }
  875. static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
  876. {
  877. struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
  878. int result = 0;
  879. if (parent)
  880. result = i2c_check_mux_parents(parent, addr);
  881. if (!result)
  882. result = device_for_each_child(&adapter->dev, &addr,
  883. i2c_check_mux_children);
  884. return result;
  885. }
  886. /**
  887. * i2c_adapter_lock_bus - Get exclusive access to an I2C bus segment
  888. * @adapter: Target I2C bus segment
  889. * @flags: I2C_LOCK_ROOT_ADAPTER locks the root i2c adapter, I2C_LOCK_SEGMENT
  890. * locks only this branch in the adapter tree
  891. */
  892. static void i2c_adapter_lock_bus(struct i2c_adapter *adapter,
  893. unsigned int flags)
  894. {
  895. rt_mutex_lock(&adapter->bus_lock);
  896. }
  897. /**
  898. * i2c_adapter_trylock_bus - Try to get exclusive access to an I2C bus segment
  899. * @adapter: Target I2C bus segment
  900. * @flags: I2C_LOCK_ROOT_ADAPTER trylocks the root i2c adapter, I2C_LOCK_SEGMENT
  901. * trylocks only this branch in the adapter tree
  902. */
  903. static int i2c_adapter_trylock_bus(struct i2c_adapter *adapter,
  904. unsigned int flags)
  905. {
  906. return rt_mutex_trylock(&adapter->bus_lock);
  907. }
  908. /**
  909. * i2c_adapter_unlock_bus - Release exclusive access to an I2C bus segment
  910. * @adapter: Target I2C bus segment
  911. * @flags: I2C_LOCK_ROOT_ADAPTER unlocks the root i2c adapter, I2C_LOCK_SEGMENT
  912. * unlocks only this branch in the adapter tree
  913. */
  914. static void i2c_adapter_unlock_bus(struct i2c_adapter *adapter,
  915. unsigned int flags)
  916. {
  917. rt_mutex_unlock(&adapter->bus_lock);
  918. }
  919. static void i2c_dev_set_name(struct i2c_adapter *adap,
  920. struct i2c_client *client)
  921. {
  922. struct acpi_device *adev = ACPI_COMPANION(&client->dev);
  923. if (adev) {
  924. dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
  925. return;
  926. }
  927. dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
  928. i2c_encode_flags_to_addr(client));
  929. }
  930. /**
  931. * i2c_new_device - instantiate an i2c device
  932. * @adap: the adapter managing the device
  933. * @info: describes one I2C device; bus_num is ignored
  934. * Context: can sleep
  935. *
  936. * Create an i2c device. Binding is handled through driver model
  937. * probe()/remove() methods. A driver may be bound to this device when we
  938. * return from this function, or any later moment (e.g. maybe hotplugging will
  939. * load the driver module). This call is not appropriate for use by mainboard
  940. * initialization logic, which usually runs during an arch_initcall() long
  941. * before any i2c_adapter could exist.
  942. *
  943. * This returns the new i2c client, which may be saved for later use with
  944. * i2c_unregister_device(); or NULL to indicate an error.
  945. */
  946. struct i2c_client *
  947. i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
  948. {
  949. struct i2c_client *client;
  950. int status;
  951. client = kzalloc(sizeof *client, GFP_KERNEL);
  952. if (!client)
  953. return NULL;
  954. client->adapter = adap;
  955. client->dev.platform_data = info->platform_data;
  956. if (info->archdata)
  957. client->dev.archdata = *info->archdata;
  958. client->flags = info->flags;
  959. client->addr = info->addr;
  960. client->irq = info->irq;
  961. strlcpy(client->name, info->type, sizeof(client->name));
  962. status = i2c_check_addr_validity(client->addr, client->flags);
  963. if (status) {
  964. dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
  965. client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
  966. goto out_err_silent;
  967. }
  968. /* Check for address business */
  969. status = i2c_check_addr_busy(adap, i2c_encode_flags_to_addr(client));
  970. if (status)
  971. goto out_err;
  972. client->dev.parent = &client->adapter->dev;
  973. client->dev.bus = &i2c_bus_type;
  974. client->dev.type = &i2c_client_type;
  975. client->dev.of_node = info->of_node;
  976. client->dev.fwnode = info->fwnode;
  977. i2c_dev_set_name(adap, client);
  978. status = device_register(&client->dev);
  979. if (status)
  980. goto out_err;
  981. dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
  982. client->name, dev_name(&client->dev));
  983. return client;
  984. out_err:
  985. dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
  986. "(%d)\n", client->name, client->addr, status);
  987. out_err_silent:
  988. kfree(client);
  989. return NULL;
  990. }
  991. EXPORT_SYMBOL_GPL(i2c_new_device);
  992. /**
  993. * i2c_unregister_device - reverse effect of i2c_new_device()
  994. * @client: value returned from i2c_new_device()
  995. * Context: can sleep
  996. */
  997. void i2c_unregister_device(struct i2c_client *client)
  998. {
  999. if (client->dev.of_node)
  1000. of_node_clear_flag(client->dev.of_node, OF_POPULATED);
  1001. if (ACPI_COMPANION(&client->dev))
  1002. acpi_device_clear_enumerated(ACPI_COMPANION(&client->dev));
  1003. device_unregister(&client->dev);
  1004. }
  1005. EXPORT_SYMBOL_GPL(i2c_unregister_device);
  1006. static const struct i2c_device_id dummy_id[] = {
  1007. { "dummy", 0 },
  1008. { },
  1009. };
  1010. static int dummy_probe(struct i2c_client *client,
  1011. const struct i2c_device_id *id)
  1012. {
  1013. return 0;
  1014. }
  1015. static int dummy_remove(struct i2c_client *client)
  1016. {
  1017. return 0;
  1018. }
  1019. static struct i2c_driver dummy_driver = {
  1020. .driver.name = "dummy",
  1021. .probe = dummy_probe,
  1022. .remove = dummy_remove,
  1023. .id_table = dummy_id,
  1024. };
  1025. /**
  1026. * i2c_new_dummy - return a new i2c device bound to a dummy driver
  1027. * @adapter: the adapter managing the device
  1028. * @address: seven bit address to be used
  1029. * Context: can sleep
  1030. *
  1031. * This returns an I2C client bound to the "dummy" driver, intended for use
  1032. * with devices that consume multiple addresses. Examples of such chips
  1033. * include various EEPROMS (like 24c04 and 24c08 models).
  1034. *
  1035. * These dummy devices have two main uses. First, most I2C and SMBus calls
  1036. * except i2c_transfer() need a client handle; the dummy will be that handle.
  1037. * And second, this prevents the specified address from being bound to a
  1038. * different driver.
  1039. *
  1040. * This returns the new i2c client, which should be saved for later use with
  1041. * i2c_unregister_device(); or NULL to indicate an error.
  1042. */
  1043. struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
  1044. {
  1045. struct i2c_board_info info = {
  1046. I2C_BOARD_INFO("dummy", address),
  1047. };
  1048. return i2c_new_device(adapter, &info);
  1049. }
  1050. EXPORT_SYMBOL_GPL(i2c_new_dummy);
  1051. /**
  1052. * i2c_new_secondary_device - Helper to get the instantiated secondary address
  1053. * and create the associated device
  1054. * @client: Handle to the primary client
  1055. * @name: Handle to specify which secondary address to get
  1056. * @default_addr: Used as a fallback if no secondary address was specified
  1057. * Context: can sleep
  1058. *
  1059. * I2C clients can be composed of multiple I2C slaves bound together in a single
  1060. * component. The I2C client driver then binds to the master I2C slave and needs
  1061. * to create I2C dummy clients to communicate with all the other slaves.
  1062. *
  1063. * This function creates and returns an I2C dummy client whose I2C address is
  1064. * retrieved from the platform firmware based on the given slave name. If no
  1065. * address is specified by the firmware default_addr is used.
  1066. *
  1067. * On DT-based platforms the address is retrieved from the "reg" property entry
  1068. * cell whose "reg-names" value matches the slave name.
  1069. *
  1070. * This returns the new i2c client, which should be saved for later use with
  1071. * i2c_unregister_device(); or NULL to indicate an error.
  1072. */
  1073. struct i2c_client *i2c_new_secondary_device(struct i2c_client *client,
  1074. const char *name,
  1075. u16 default_addr)
  1076. {
  1077. struct device_node *np = client->dev.of_node;
  1078. u32 addr = default_addr;
  1079. int i;
  1080. if (np) {
  1081. i = of_property_match_string(np, "reg-names", name);
  1082. if (i >= 0)
  1083. of_property_read_u32_index(np, "reg", i, &addr);
  1084. }
  1085. dev_dbg(&client->adapter->dev, "Address for %s : 0x%x\n", name, addr);
  1086. return i2c_new_dummy(client->adapter, addr);
  1087. }
  1088. EXPORT_SYMBOL_GPL(i2c_new_secondary_device);
  1089. /* ------------------------------------------------------------------------- */
  1090. /* I2C bus adapters -- one roots each I2C or SMBUS segment */
  1091. static void i2c_adapter_dev_release(struct device *dev)
  1092. {
  1093. struct i2c_adapter *adap = to_i2c_adapter(dev);
  1094. complete(&adap->dev_released);
  1095. }
  1096. /*
  1097. * This function is only needed for mutex_lock_nested, so it is never
  1098. * called unless locking correctness checking is enabled. Thus we
  1099. * make it inline to avoid a compiler warning. That's what gcc ends up
  1100. * doing anyway.
  1101. */
  1102. static inline unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
  1103. {
  1104. unsigned int depth = 0;
  1105. while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
  1106. depth++;
  1107. return depth;
  1108. }
  1109. /*
  1110. * Let users instantiate I2C devices through sysfs. This can be used when
  1111. * platform initialization code doesn't contain the proper data for
  1112. * whatever reason. Also useful for drivers that do device detection and
  1113. * detection fails, either because the device uses an unexpected address,
  1114. * or this is a compatible device with different ID register values.
  1115. *
  1116. * Parameter checking may look overzealous, but we really don't want
  1117. * the user to provide incorrect parameters.
  1118. */
  1119. static ssize_t
  1120. i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
  1121. const char *buf, size_t count)
  1122. {
  1123. struct i2c_adapter *adap = to_i2c_adapter(dev);
  1124. struct i2c_board_info info;
  1125. struct i2c_client *client;
  1126. char *blank, end;
  1127. int res;
  1128. memset(&info, 0, sizeof(struct i2c_board_info));
  1129. blank = strchr(buf, ' ');
  1130. if (!blank) {
  1131. dev_err(dev, "%s: Missing parameters\n", "new_device");
  1132. return -EINVAL;
  1133. }
  1134. if (blank - buf > I2C_NAME_SIZE - 1) {
  1135. dev_err(dev, "%s: Invalid device name\n", "new_device");
  1136. return -EINVAL;
  1137. }
  1138. memcpy(info.type, buf, blank - buf);
  1139. /* Parse remaining parameters, reject extra parameters */
  1140. res = sscanf(++blank, "%hi%c", &info.addr, &end);
  1141. if (res < 1) {
  1142. dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
  1143. return -EINVAL;
  1144. }
  1145. if (res > 1 && end != '\n') {
  1146. dev_err(dev, "%s: Extra parameters\n", "new_device");
  1147. return -EINVAL;
  1148. }
  1149. if ((info.addr & I2C_ADDR_OFFSET_TEN_BIT) == I2C_ADDR_OFFSET_TEN_BIT) {
  1150. info.addr &= ~I2C_ADDR_OFFSET_TEN_BIT;
  1151. info.flags |= I2C_CLIENT_TEN;
  1152. }
  1153. if (info.addr & I2C_ADDR_OFFSET_SLAVE) {
  1154. info.addr &= ~I2C_ADDR_OFFSET_SLAVE;
  1155. info.flags |= I2C_CLIENT_SLAVE;
  1156. }
  1157. client = i2c_new_device(adap, &info);
  1158. if (!client)
  1159. return -EINVAL;
  1160. /* Keep track of the added device */
  1161. mutex_lock(&adap->userspace_clients_lock);
  1162. list_add_tail(&client->detected, &adap->userspace_clients);
  1163. mutex_unlock(&adap->userspace_clients_lock);
  1164. dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
  1165. info.type, info.addr);
  1166. return count;
  1167. }
  1168. static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
  1169. /*
  1170. * And of course let the users delete the devices they instantiated, if
  1171. * they got it wrong. This interface can only be used to delete devices
  1172. * instantiated by i2c_sysfs_new_device above. This guarantees that we
  1173. * don't delete devices to which some kernel code still has references.
  1174. *
  1175. * Parameter checking may look overzealous, but we really don't want
  1176. * the user to delete the wrong device.
  1177. */
  1178. static ssize_t
  1179. i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
  1180. const char *buf, size_t count)
  1181. {
  1182. struct i2c_adapter *adap = to_i2c_adapter(dev);
  1183. struct i2c_client *client, *next;
  1184. unsigned short addr;
  1185. char end;
  1186. int res;
  1187. /* Parse parameters, reject extra parameters */
  1188. res = sscanf(buf, "%hi%c", &addr, &end);
  1189. if (res < 1) {
  1190. dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
  1191. return -EINVAL;
  1192. }
  1193. if (res > 1 && end != '\n') {
  1194. dev_err(dev, "%s: Extra parameters\n", "delete_device");
  1195. return -EINVAL;
  1196. }
  1197. /* Make sure the device was added through sysfs */
  1198. res = -ENOENT;
  1199. mutex_lock_nested(&adap->userspace_clients_lock,
  1200. i2c_adapter_depth(adap));
  1201. list_for_each_entry_safe(client, next, &adap->userspace_clients,
  1202. detected) {
  1203. if (i2c_encode_flags_to_addr(client) == addr) {
  1204. dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
  1205. "delete_device", client->name, client->addr);
  1206. list_del(&client->detected);
  1207. i2c_unregister_device(client);
  1208. res = count;
  1209. break;
  1210. }
  1211. }
  1212. mutex_unlock(&adap->userspace_clients_lock);
  1213. if (res < 0)
  1214. dev_err(dev, "%s: Can't find device in list\n",
  1215. "delete_device");
  1216. return res;
  1217. }
  1218. static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
  1219. i2c_sysfs_delete_device);
  1220. static struct attribute *i2c_adapter_attrs[] = {
  1221. &dev_attr_name.attr,
  1222. &dev_attr_new_device.attr,
  1223. &dev_attr_delete_device.attr,
  1224. NULL
  1225. };
  1226. ATTRIBUTE_GROUPS(i2c_adapter);
  1227. struct device_type i2c_adapter_type = {
  1228. .groups = i2c_adapter_groups,
  1229. .release = i2c_adapter_dev_release,
  1230. };
  1231. EXPORT_SYMBOL_GPL(i2c_adapter_type);
  1232. /**
  1233. * i2c_verify_adapter - return parameter as i2c_adapter or NULL
  1234. * @dev: device, probably from some driver model iterator
  1235. *
  1236. * When traversing the driver model tree, perhaps using driver model
  1237. * iterators like @device_for_each_child(), you can't assume very much
  1238. * about the nodes you find. Use this function to avoid oopses caused
  1239. * by wrongly treating some non-I2C device as an i2c_adapter.
  1240. */
  1241. struct i2c_adapter *i2c_verify_adapter(struct device *dev)
  1242. {
  1243. return (dev->type == &i2c_adapter_type)
  1244. ? to_i2c_adapter(dev)
  1245. : NULL;
  1246. }
  1247. EXPORT_SYMBOL(i2c_verify_adapter);
  1248. #ifdef CONFIG_I2C_COMPAT
  1249. static struct class_compat *i2c_adapter_compat_class;
  1250. #endif
  1251. static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
  1252. {
  1253. struct i2c_devinfo *devinfo;
  1254. down_read(&__i2c_board_lock);
  1255. list_for_each_entry(devinfo, &__i2c_board_list, list) {
  1256. if (devinfo->busnum == adapter->nr
  1257. && !i2c_new_device(adapter,
  1258. &devinfo->board_info))
  1259. dev_err(&adapter->dev,
  1260. "Can't create device at 0x%02x\n",
  1261. devinfo->board_info.addr);
  1262. }
  1263. up_read(&__i2c_board_lock);
  1264. }
  1265. /* OF support code */
  1266. #if IS_ENABLED(CONFIG_OF)
  1267. static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
  1268. struct device_node *node)
  1269. {
  1270. struct i2c_client *result;
  1271. struct i2c_board_info info = {};
  1272. struct dev_archdata dev_ad = {};
  1273. const __be32 *addr_be;
  1274. u32 addr;
  1275. int len;
  1276. dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
  1277. if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
  1278. dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
  1279. node->full_name);
  1280. return ERR_PTR(-EINVAL);
  1281. }
  1282. addr_be = of_get_property(node, "reg", &len);
  1283. if (!addr_be || (len < sizeof(*addr_be))) {
  1284. dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
  1285. node->full_name);
  1286. return ERR_PTR(-EINVAL);
  1287. }
  1288. addr = be32_to_cpup(addr_be);
  1289. if (addr & I2C_TEN_BIT_ADDRESS) {
  1290. addr &= ~I2C_TEN_BIT_ADDRESS;
  1291. info.flags |= I2C_CLIENT_TEN;
  1292. }
  1293. if (addr & I2C_OWN_SLAVE_ADDRESS) {
  1294. addr &= ~I2C_OWN_SLAVE_ADDRESS;
  1295. info.flags |= I2C_CLIENT_SLAVE;
  1296. }
  1297. if (i2c_check_addr_validity(addr, info.flags)) {
  1298. dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
  1299. info.addr, node->full_name);
  1300. return ERR_PTR(-EINVAL);
  1301. }
  1302. info.addr = addr;
  1303. info.of_node = of_node_get(node);
  1304. info.archdata = &dev_ad;
  1305. if (of_get_property(node, "wakeup-source", NULL))
  1306. info.flags |= I2C_CLIENT_WAKE;
  1307. result = i2c_new_device(adap, &info);
  1308. if (result == NULL) {
  1309. dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
  1310. node->full_name);
  1311. of_node_put(node);
  1312. return ERR_PTR(-EINVAL);
  1313. }
  1314. return result;
  1315. }
  1316. static void of_i2c_register_devices(struct i2c_adapter *adap)
  1317. {
  1318. struct device_node *node;
  1319. /* Only register child devices if the adapter has a node pointer set */
  1320. if (!adap->dev.of_node)
  1321. return;
  1322. dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
  1323. for_each_available_child_of_node(adap->dev.of_node, node) {
  1324. if (of_node_test_and_set_flag(node, OF_POPULATED))
  1325. continue;
  1326. of_i2c_register_device(adap, node);
  1327. }
  1328. }
  1329. static int of_dev_node_match(struct device *dev, void *data)
  1330. {
  1331. return dev->of_node == data;
  1332. }
  1333. /* must call put_device() when done with returned i2c_client device */
  1334. struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
  1335. {
  1336. struct device *dev;
  1337. struct i2c_client *client;
  1338. dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
  1339. if (!dev)
  1340. return NULL;
  1341. client = i2c_verify_client(dev);
  1342. if (!client)
  1343. put_device(dev);
  1344. return client;
  1345. }
  1346. EXPORT_SYMBOL(of_find_i2c_device_by_node);
  1347. /* must call put_device() when done with returned i2c_adapter device */
  1348. struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
  1349. {
  1350. struct device *dev;
  1351. struct i2c_adapter *adapter;
  1352. dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
  1353. if (!dev)
  1354. return NULL;
  1355. adapter = i2c_verify_adapter(dev);
  1356. if (!adapter)
  1357. put_device(dev);
  1358. return adapter;
  1359. }
  1360. EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
  1361. /* must call i2c_put_adapter() when done with returned i2c_adapter device */
  1362. struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node)
  1363. {
  1364. struct i2c_adapter *adapter;
  1365. adapter = of_find_i2c_adapter_by_node(node);
  1366. if (!adapter)
  1367. return NULL;
  1368. if (!try_module_get(adapter->owner)) {
  1369. put_device(&adapter->dev);
  1370. adapter = NULL;
  1371. }
  1372. return adapter;
  1373. }
  1374. EXPORT_SYMBOL(of_get_i2c_adapter_by_node);
  1375. #else
  1376. static void of_i2c_register_devices(struct i2c_adapter *adap) { }
  1377. #endif /* CONFIG_OF */
  1378. static int i2c_do_add_adapter(struct i2c_driver *driver,
  1379. struct i2c_adapter *adap)
  1380. {
  1381. /* Detect supported devices on that bus, and instantiate them */
  1382. i2c_detect(adap, driver);
  1383. /* Let legacy drivers scan this bus for matching devices */
  1384. if (driver->attach_adapter) {
  1385. dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
  1386. driver->driver.name);
  1387. dev_warn(&adap->dev, "Please use another way to instantiate "
  1388. "your i2c_client\n");
  1389. /* We ignore the return code; if it fails, too bad */
  1390. driver->attach_adapter(adap);
  1391. }
  1392. return 0;
  1393. }
  1394. static int __process_new_adapter(struct device_driver *d, void *data)
  1395. {
  1396. return i2c_do_add_adapter(to_i2c_driver(d), data);
  1397. }
  1398. static int i2c_register_adapter(struct i2c_adapter *adap)
  1399. {
  1400. int res = -EINVAL;
  1401. /* Can't register until after driver model init */
  1402. if (WARN_ON(!is_registered)) {
  1403. res = -EAGAIN;
  1404. goto out_list;
  1405. }
  1406. /* Sanity checks */
  1407. if (WARN(!adap->name[0], "i2c adapter has no name"))
  1408. goto out_list;
  1409. if (!adap->algo) {
  1410. pr_err("adapter '%s': no algo supplied!\n", adap->name);
  1411. goto out_list;
  1412. }
  1413. if (!adap->lock_bus) {
  1414. adap->lock_bus = i2c_adapter_lock_bus;
  1415. adap->trylock_bus = i2c_adapter_trylock_bus;
  1416. adap->unlock_bus = i2c_adapter_unlock_bus;
  1417. }
  1418. rt_mutex_init(&adap->bus_lock);
  1419. rt_mutex_init(&adap->mux_lock);
  1420. mutex_init(&adap->userspace_clients_lock);
  1421. INIT_LIST_HEAD(&adap->userspace_clients);
  1422. /* Set default timeout to 1 second if not already set */
  1423. if (adap->timeout == 0)
  1424. adap->timeout = HZ;
  1425. dev_set_name(&adap->dev, "i2c-%d", adap->nr);
  1426. adap->dev.bus = &i2c_bus_type;
  1427. adap->dev.type = &i2c_adapter_type;
  1428. res = device_register(&adap->dev);
  1429. if (res) {
  1430. pr_err("adapter '%s': can't register device (%d)\n", adap->name, res);
  1431. goto out_list;
  1432. }
  1433. dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
  1434. pm_runtime_no_callbacks(&adap->dev);
  1435. pm_suspend_ignore_children(&adap->dev, true);
  1436. pm_runtime_enable(&adap->dev);
  1437. #ifdef CONFIG_I2C_COMPAT
  1438. res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
  1439. adap->dev.parent);
  1440. if (res)
  1441. dev_warn(&adap->dev,
  1442. "Failed to create compatibility class link\n");
  1443. #endif
  1444. i2c_init_recovery(adap);
  1445. /* create pre-declared device nodes */
  1446. of_i2c_register_devices(adap);
  1447. acpi_i2c_register_devices(adap);
  1448. acpi_i2c_install_space_handler(adap);
  1449. if (adap->nr < __i2c_first_dynamic_bus_num)
  1450. i2c_scan_static_board_info(adap);
  1451. /* Notify drivers */
  1452. mutex_lock(&core_lock);
  1453. bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
  1454. mutex_unlock(&core_lock);
  1455. return 0;
  1456. out_list:
  1457. mutex_lock(&core_lock);
  1458. idr_remove(&i2c_adapter_idr, adap->nr);
  1459. mutex_unlock(&core_lock);
  1460. return res;
  1461. }
  1462. /**
  1463. * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
  1464. * @adap: the adapter to register (with adap->nr initialized)
  1465. * Context: can sleep
  1466. *
  1467. * See i2c_add_numbered_adapter() for details.
  1468. */
  1469. static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
  1470. {
  1471. int id;
  1472. mutex_lock(&core_lock);
  1473. id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1, GFP_KERNEL);
  1474. mutex_unlock(&core_lock);
  1475. if (WARN(id < 0, "couldn't get idr"))
  1476. return id == -ENOSPC ? -EBUSY : id;
  1477. return i2c_register_adapter(adap);
  1478. }
  1479. /**
  1480. * i2c_add_adapter - declare i2c adapter, use dynamic bus number
  1481. * @adapter: the adapter to add
  1482. * Context: can sleep
  1483. *
  1484. * This routine is used to declare an I2C adapter when its bus number
  1485. * doesn't matter or when its bus number is specified by an dt alias.
  1486. * Examples of bases when the bus number doesn't matter: I2C adapters
  1487. * dynamically added by USB links or PCI plugin cards.
  1488. *
  1489. * When this returns zero, a new bus number was allocated and stored
  1490. * in adap->nr, and the specified adapter became available for clients.
  1491. * Otherwise, a negative errno value is returned.
  1492. */
  1493. int i2c_add_adapter(struct i2c_adapter *adapter)
  1494. {
  1495. struct device *dev = &adapter->dev;
  1496. int id;
  1497. if (dev->of_node) {
  1498. id = of_alias_get_id(dev->of_node, "i2c");
  1499. if (id >= 0) {
  1500. adapter->nr = id;
  1501. return __i2c_add_numbered_adapter(adapter);
  1502. }
  1503. }
  1504. mutex_lock(&core_lock);
  1505. id = idr_alloc(&i2c_adapter_idr, adapter,
  1506. __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
  1507. mutex_unlock(&core_lock);
  1508. if (WARN(id < 0, "couldn't get idr"))
  1509. return id;
  1510. adapter->nr = id;
  1511. return i2c_register_adapter(adapter);
  1512. }
  1513. EXPORT_SYMBOL(i2c_add_adapter);
  1514. /**
  1515. * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
  1516. * @adap: the adapter to register (with adap->nr initialized)
  1517. * Context: can sleep
  1518. *
  1519. * This routine is used to declare an I2C adapter when its bus number
  1520. * matters. For example, use it for I2C adapters from system-on-chip CPUs,
  1521. * or otherwise built in to the system's mainboard, and where i2c_board_info
  1522. * is used to properly configure I2C devices.
  1523. *
  1524. * If the requested bus number is set to -1, then this function will behave
  1525. * identically to i2c_add_adapter, and will dynamically assign a bus number.
  1526. *
  1527. * If no devices have pre-been declared for this bus, then be sure to
  1528. * register the adapter before any dynamically allocated ones. Otherwise
  1529. * the required bus ID may not be available.
  1530. *
  1531. * When this returns zero, the specified adapter became available for
  1532. * clients using the bus number provided in adap->nr. Also, the table
  1533. * of I2C devices pre-declared using i2c_register_board_info() is scanned,
  1534. * and the appropriate driver model device nodes are created. Otherwise, a
  1535. * negative errno value is returned.
  1536. */
  1537. int i2c_add_numbered_adapter(struct i2c_adapter *adap)
  1538. {
  1539. if (adap->nr == -1) /* -1 means dynamically assign bus id */
  1540. return i2c_add_adapter(adap);
  1541. return __i2c_add_numbered_adapter(adap);
  1542. }
  1543. EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
  1544. static void i2c_do_del_adapter(struct i2c_driver *driver,
  1545. struct i2c_adapter *adapter)
  1546. {
  1547. struct i2c_client *client, *_n;
  1548. /* Remove the devices we created ourselves as the result of hardware
  1549. * probing (using a driver's detect method) */
  1550. list_for_each_entry_safe(client, _n, &driver->clients, detected) {
  1551. if (client->adapter == adapter) {
  1552. dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
  1553. client->name, client->addr);
  1554. list_del(&client->detected);
  1555. i2c_unregister_device(client);
  1556. }
  1557. }
  1558. }
  1559. static int __unregister_client(struct device *dev, void *dummy)
  1560. {
  1561. struct i2c_client *client = i2c_verify_client(dev);
  1562. if (client && strcmp(client->name, "dummy"))
  1563. i2c_unregister_device(client);
  1564. return 0;
  1565. }
  1566. static int __unregister_dummy(struct device *dev, void *dummy)
  1567. {
  1568. struct i2c_client *client = i2c_verify_client(dev);
  1569. if (client)
  1570. i2c_unregister_device(client);
  1571. return 0;
  1572. }
  1573. static int __process_removed_adapter(struct device_driver *d, void *data)
  1574. {
  1575. i2c_do_del_adapter(to_i2c_driver(d), data);
  1576. return 0;
  1577. }
  1578. /**
  1579. * i2c_del_adapter - unregister I2C adapter
  1580. * @adap: the adapter being unregis…