PageRenderTime 75ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/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
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.0
  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 unregistered
  1581. * Context: can sleep
  1582. *
  1583. * This unregisters an I2C adapter which was previously registered
  1584. * by @i2c_add_adapter or @i2c_add_numbered_adapter.
  1585. */
  1586. void i2c_del_adapter(struct i2c_adapter *adap)
  1587. {
  1588. struct i2c_adapter *found;
  1589. struct i2c_client *client, *next;
  1590. /* First make sure that this adapter was ever added */
  1591. mutex_lock(&core_lock);
  1592. found = idr_find(&i2c_adapter_idr, adap->nr);
  1593. mutex_unlock(&core_lock);
  1594. if (found != adap) {
  1595. pr_debug("attempting to delete unregistered adapter [%s]\n", adap->name);
  1596. return;
  1597. }
  1598. acpi_i2c_remove_space_handler(adap);
  1599. /* Tell drivers about this removal */
  1600. mutex_lock(&core_lock);
  1601. bus_for_each_drv(&i2c_bus_type, NULL, adap,
  1602. __process_removed_adapter);
  1603. mutex_unlock(&core_lock);
  1604. /* Remove devices instantiated from sysfs */
  1605. mutex_lock_nested(&adap->userspace_clients_lock,
  1606. i2c_adapter_depth(adap));
  1607. list_for_each_entry_safe(client, next, &adap->userspace_clients,
  1608. detected) {
  1609. dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
  1610. client->addr);
  1611. list_del(&client->detected);
  1612. i2c_unregister_device(client);
  1613. }
  1614. mutex_unlock(&adap->userspace_clients_lock);
  1615. /* Detach any active clients. This can't fail, thus we do not
  1616. * check the returned value. This is a two-pass process, because
  1617. * we can't remove the dummy devices during the first pass: they
  1618. * could have been instantiated by real devices wishing to clean
  1619. * them up properly, so we give them a chance to do that first. */
  1620. device_for_each_child(&adap->dev, NULL, __unregister_client);
  1621. device_for_each_child(&adap->dev, NULL, __unregister_dummy);
  1622. #ifdef CONFIG_I2C_COMPAT
  1623. class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
  1624. adap->dev.parent);
  1625. #endif
  1626. /* device name is gone after device_unregister */
  1627. dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
  1628. pm_runtime_disable(&adap->dev);
  1629. /* wait until all references to the device are gone
  1630. *
  1631. * FIXME: This is old code and should ideally be replaced by an
  1632. * alternative which results in decoupling the lifetime of the struct
  1633. * device from the i2c_adapter, like spi or netdev do. Any solution
  1634. * should be thoroughly tested with DEBUG_KOBJECT_RELEASE enabled!
  1635. */
  1636. init_completion(&adap->dev_released);
  1637. device_unregister(&adap->dev);
  1638. wait_for_completion(&adap->dev_released);
  1639. /* free bus id */
  1640. mutex_lock(&core_lock);
  1641. idr_remove(&i2c_adapter_idr, adap->nr);
  1642. mutex_unlock(&core_lock);
  1643. /* Clear the device structure in case this adapter is ever going to be
  1644. added again */
  1645. memset(&adap->dev, 0, sizeof(adap->dev));
  1646. }
  1647. EXPORT_SYMBOL(i2c_del_adapter);
  1648. /**
  1649. * i2c_parse_fw_timings - get I2C related timing parameters from firmware
  1650. * @dev: The device to scan for I2C timing properties
  1651. * @t: the i2c_timings struct to be filled with values
  1652. * @use_defaults: bool to use sane defaults derived from the I2C specification
  1653. * when properties are not found, otherwise use 0
  1654. *
  1655. * Scan the device for the generic I2C properties describing timing parameters
  1656. * for the signal and fill the given struct with the results. If a property was
  1657. * not found and use_defaults was true, then maximum timings are assumed which
  1658. * are derived from the I2C specification. If use_defaults is not used, the
  1659. * results will be 0, so drivers can apply their own defaults later. The latter
  1660. * is mainly intended for avoiding regressions of existing drivers which want
  1661. * to switch to this function. New drivers almost always should use the defaults.
  1662. */
  1663. void i2c_parse_fw_timings(struct device *dev, struct i2c_timings *t, bool use_defaults)
  1664. {
  1665. int ret;
  1666. memset(t, 0, sizeof(*t));
  1667. ret = device_property_read_u32(dev, "clock-frequency", &t->bus_freq_hz);
  1668. if (ret && use_defaults)
  1669. t->bus_freq_hz = 100000;
  1670. ret = device_property_read_u32(dev, "i2c-scl-rising-time-ns", &t->scl_rise_ns);
  1671. if (ret && use_defaults) {
  1672. if (t->bus_freq_hz <= 100000)
  1673. t->scl_rise_ns = 1000;
  1674. else if (t->bus_freq_hz <= 400000)
  1675. t->scl_rise_ns = 300;
  1676. else
  1677. t->scl_rise_ns = 120;
  1678. }
  1679. ret = device_property_read_u32(dev, "i2c-scl-falling-time-ns", &t->scl_fall_ns);
  1680. if (ret && use_defaults) {
  1681. if (t->bus_freq_hz <= 400000)
  1682. t->scl_fall_ns = 300;
  1683. else
  1684. t->scl_fall_ns = 120;
  1685. }
  1686. device_property_read_u32(dev, "i2c-scl-internal-delay-ns", &t->scl_int_delay_ns);
  1687. ret = device_property_read_u32(dev, "i2c-sda-falling-time-ns", &t->sda_fall_ns);
  1688. if (ret && use_defaults)
  1689. t->sda_fall_ns = t->scl_fall_ns;
  1690. }
  1691. EXPORT_SYMBOL_GPL(i2c_parse_fw_timings);
  1692. /* ------------------------------------------------------------------------- */
  1693. int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
  1694. {
  1695. int res;
  1696. mutex_lock(&core_lock);
  1697. res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
  1698. mutex_unlock(&core_lock);
  1699. return res;
  1700. }
  1701. EXPORT_SYMBOL_GPL(i2c_for_each_dev);
  1702. static int __process_new_driver(struct device *dev, void *data)
  1703. {
  1704. if (dev->type != &i2c_adapter_type)
  1705. return 0;
  1706. return i2c_do_add_adapter(data, to_i2c_adapter(dev));
  1707. }
  1708. /*
  1709. * An i2c_driver is used with one or more i2c_client (device) nodes to access
  1710. * i2c slave chips, on a bus instance associated with some i2c_adapter.
  1711. */
  1712. int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
  1713. {
  1714. int res;
  1715. /* Can't register until after driver model init */
  1716. if (WARN_ON(!is_registered))
  1717. return -EAGAIN;
  1718. /* add the driver to the list of i2c drivers in the driver core */
  1719. driver->driver.owner = owner;
  1720. driver->driver.bus = &i2c_bus_type;
  1721. /* When registration returns, the driver core
  1722. * will have called probe() for all matching-but-unbound devices.
  1723. */
  1724. res = driver_register(&driver->driver);
  1725. if (res)
  1726. return res;
  1727. pr_debug("driver [%s] registered\n", driver->driver.name);
  1728. INIT_LIST_HEAD(&driver->clients);
  1729. /* Walk the adapters that are already present */
  1730. i2c_for_each_dev(driver, __process_new_driver);
  1731. return 0;
  1732. }
  1733. EXPORT_SYMBOL(i2c_register_driver);
  1734. static int __process_removed_driver(struct device *dev, void *data)
  1735. {
  1736. if (dev->type == &i2c_adapter_type)
  1737. i2c_do_del_adapter(data, to_i2c_adapter(dev));
  1738. return 0;
  1739. }
  1740. /**
  1741. * i2c_del_driver - unregister I2C driver
  1742. * @driver: the driver being unregistered
  1743. * Context: can sleep
  1744. */
  1745. void i2c_del_driver(struct i2c_driver *driver)
  1746. {
  1747. i2c_for_each_dev(driver, __process_removed_driver);
  1748. driver_unregister(&driver->driver);
  1749. pr_debug("driver [%s] unregistered\n", driver->driver.name);
  1750. }
  1751. EXPORT_SYMBOL(i2c_del_driver);
  1752. /* ------------------------------------------------------------------------- */
  1753. /**
  1754. * i2c_use_client - increments the reference count of the i2c client structure
  1755. * @client: the client being referenced
  1756. *
  1757. * Each live reference to a client should be refcounted. The driver model does
  1758. * that automatically as part of driver binding, so that most drivers don't
  1759. * need to do this explicitly: they hold a reference until they're unbound
  1760. * from the device.
  1761. *
  1762. * A pointer to the client with the incremented reference counter is returned.
  1763. */
  1764. struct i2c_client *i2c_use_client(struct i2c_client *client)
  1765. {
  1766. if (client && get_device(&client->dev))
  1767. return client;
  1768. return NULL;
  1769. }
  1770. EXPORT_SYMBOL(i2c_use_client);
  1771. /**
  1772. * i2c_release_client - release a use of the i2c client structure
  1773. * @client: the client being no longer referenced
  1774. *
  1775. * Must be called when a user of a client is finished with it.
  1776. */
  1777. void i2c_release_client(struct i2c_client *client)
  1778. {
  1779. if (client)
  1780. put_device(&client->dev);
  1781. }
  1782. EXPORT_SYMBOL(i2c_release_client);
  1783. struct i2c_cmd_arg {
  1784. unsigned cmd;
  1785. void *arg;
  1786. };
  1787. static int i2c_cmd(struct device *dev, void *_arg)
  1788. {
  1789. struct i2c_client *client = i2c_verify_client(dev);
  1790. struct i2c_cmd_arg *arg = _arg;
  1791. struct i2c_driver *driver;
  1792. if (!client || !client->dev.driver)
  1793. return 0;
  1794. driver = to_i2c_driver(client->dev.driver);
  1795. if (driver->command)
  1796. driver->command(client, arg->cmd, arg->arg);
  1797. return 0;
  1798. }
  1799. void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
  1800. {
  1801. struct i2c_cmd_arg cmd_arg;
  1802. cmd_arg.cmd = cmd;
  1803. cmd_arg.arg = arg;
  1804. device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
  1805. }
  1806. EXPORT_SYMBOL(i2c_clients_command);
  1807. #if IS_ENABLED(CONFIG_OF_DYNAMIC)
  1808. static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
  1809. void *arg)
  1810. {
  1811. struct of_reconfig_data *rd = arg;
  1812. struct i2c_adapter *adap;
  1813. struct i2c_client *client;
  1814. switch (of_reconfig_get_state_change(action, rd)) {
  1815. case OF_RECONFIG_CHANGE_ADD:
  1816. adap = of_find_i2c_adapter_by_node(rd->dn->parent);
  1817. if (adap == NULL)
  1818. return NOTIFY_OK; /* not for us */
  1819. if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
  1820. put_device(&adap->dev);
  1821. return NOTIFY_OK;
  1822. }
  1823. client = of_i2c_register_device(adap, rd->dn);
  1824. put_device(&adap->dev);
  1825. if (IS_ERR(client)) {
  1826. dev_err(&adap->dev, "failed to create client for '%s'\n",
  1827. rd->dn->full_name);
  1828. return notifier_from_errno(PTR_ERR(client));
  1829. }
  1830. break;
  1831. case OF_RECONFIG_CHANGE_REMOVE:
  1832. /* already depopulated? */
  1833. if (!of_node_check_flag(rd->dn, OF_POPULATED))
  1834. return NOTIFY_OK;
  1835. /* find our device by node */
  1836. client = of_find_i2c_device_by_node(rd->dn);
  1837. if (client == NULL)
  1838. return NOTIFY_OK; /* no? not meant for us */
  1839. /* unregister takes one ref away */
  1840. i2c_unregister_device(client);
  1841. /* and put the reference of the find */
  1842. put_device(&client->dev);
  1843. break;
  1844. }
  1845. return NOTIFY_OK;
  1846. }
  1847. static struct notifier_block i2c_of_notifier = {
  1848. .notifier_call = of_i2c_notify,
  1849. };
  1850. #else
  1851. extern struct notifier_block i2c_of_notifier;
  1852. #endif /* CONFIG_OF_DYNAMIC */
  1853. static int __init i2c_init(void)
  1854. {
  1855. int retval;
  1856. retval = of_alias_get_highest_id("i2c");
  1857. down_write(&__i2c_board_lock);
  1858. if (retval >= __i2c_first_dynamic_bus_num)
  1859. __i2c_first_dynamic_bus_num = retval + 1;
  1860. up_write(&__i2c_board_lock);
  1861. retval = bus_register(&i2c_bus_type);
  1862. if (retval)
  1863. return retval;
  1864. is_registered = true;
  1865. #ifdef CONFIG_I2C_COMPAT
  1866. i2c_adapter_compat_class = class_compat_register("i2c-adapter");
  1867. if (!i2c_adapter_compat_class) {
  1868. retval = -ENOMEM;
  1869. goto bus_err;
  1870. }
  1871. #endif
  1872. retval = i2c_add_driver(&dummy_driver);
  1873. if (retval)
  1874. goto class_err;
  1875. if (IS_ENABLED(CONFIG_OF_DYNAMIC))
  1876. WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier));
  1877. if (IS_ENABLED(CONFIG_ACPI))
  1878. WARN_ON(acpi_reconfig_notifier_register(&i2c_acpi_notifier));
  1879. return 0;
  1880. class_err:
  1881. #ifdef CONFIG_I2C_COMPAT
  1882. class_compat_unregister(i2c_adapter_compat_class);
  1883. bus_err:
  1884. #endif
  1885. is_registered = false;
  1886. bus_unregister(&i2c_bus_type);
  1887. return retval;
  1888. }
  1889. static void __exit i2c_exit(void)
  1890. {
  1891. if (IS_ENABLED(CONFIG_ACPI))
  1892. WARN_ON(acpi_reconfig_notifier_unregister(&i2c_acpi_notifier));
  1893. if (IS_ENABLED(CONFIG_OF_DYNAMIC))
  1894. WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier));
  1895. i2c_del_driver(&dummy_driver);
  1896. #ifdef CONFIG_I2C_COMPAT
  1897. class_compat_unregister(i2c_adapter_compat_class);
  1898. #endif
  1899. bus_unregister(&i2c_bus_type);
  1900. tracepoint_synchronize_unregister();
  1901. }
  1902. /* We must initialize early, because some subsystems register i2c drivers
  1903. * in subsys_initcall() code, but are linked (and initialized) before i2c.
  1904. */
  1905. postcore_initcall(i2c_init);
  1906. module_exit(i2c_exit);
  1907. /* ----------------------------------------------------
  1908. * the functional interface to the i2c busses.
  1909. * ----------------------------------------------------
  1910. */
  1911. /* Check if val is exceeding the quirk IFF quirk is non 0 */
  1912. #define i2c_quirk_exceeded(val, quirk) ((quirk) && ((val) > (quirk)))
  1913. static int i2c_quirk_error(struct i2c_adapter *adap, struct i2c_msg *msg, char *err_msg)
  1914. {
  1915. dev_err_ratelimited(&adap->dev, "adapter quirk: %s (addr 0x%04x, size %u, %s)\n",
  1916. err_msg, msg->addr, msg->len,
  1917. msg->flags & I2C_M_RD ? "read" : "write");
  1918. return -EOPNOTSUPP;
  1919. }
  1920. static int i2c_check_for_quirks(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  1921. {
  1922. const struct i2c_adapter_quirks *q = adap->quirks;
  1923. int max_num = q->max_num_msgs, i;
  1924. bool do_len_check = true;
  1925. if (q->flags & I2C_AQ_COMB) {
  1926. max_num = 2;
  1927. /* special checks for combined messages */
  1928. if (num == 2) {
  1929. if (q->flags & I2C_AQ_COMB_WRITE_FIRST && msgs[0].flags & I2C_M_RD)
  1930. return i2c_quirk_error(adap, &msgs[0], "1st comb msg must be write");
  1931. if (q->flags & I2C_AQ_COMB_READ_SECOND && !(msgs[1].flags & I2C_M_RD))
  1932. return i2c_quirk_error(adap, &msgs[1], "2nd comb msg must be read");
  1933. if (q->flags & I2C_AQ_COMB_SAME_ADDR && msgs[0].addr != msgs[1].addr)
  1934. return i2c_quirk_error(adap, &msgs[0], "comb msg only to same addr");
  1935. if (i2c_quirk_exceeded(msgs[0].len, q->max_comb_1st_msg_len))
  1936. return i2c_quirk_error(adap, &msgs[0], "msg too long");
  1937. if (i2c_quirk_exceeded(msgs[1].len, q->max_comb_2nd_msg_len))
  1938. return i2c_quirk_error(adap, &msgs[1], "msg too long");
  1939. do_len_check = false;
  1940. }
  1941. }
  1942. if (i2c_quirk_exceeded(num, max_num))
  1943. return i2c_quirk_error(adap, &msgs[0], "too many messages");
  1944. for (i = 0; i < num; i++) {
  1945. u16 len = msgs[i].len;
  1946. if (msgs[i].flags & I2C_M_RD) {
  1947. if (do_len_check && i2c_quirk_exceeded(len, q->max_read_len))
  1948. return i2c_quirk_error(adap, &msgs[i], "msg too long");
  1949. } else {
  1950. if (do_len_check && i2c_quirk_exceeded(len, q->max_write_len))
  1951. return i2c_quirk_error(adap, &msgs[i], "msg too long");
  1952. }
  1953. }
  1954. return 0;
  1955. }
  1956. /**
  1957. * __i2c_transfer - unlocked flavor of i2c_transfer
  1958. * @adap: Handle to I2C bus
  1959. * @msgs: One or more messages to execute before STOP is issued to
  1960. * terminate the operation; each message begins with a START.
  1961. * @num: Number of messages to be executed.
  1962. *
  1963. * Returns negative errno, else the number of messages executed.
  1964. *
  1965. * Adapter lock must be held when calling this function. No debug logging
  1966. * takes place. adap->algo->master_xfer existence isn't checked.
  1967. */
  1968. int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  1969. {
  1970. unsigned long orig_jiffies;
  1971. int ret, try;
  1972. if (adap->quirks && i2c_check_for_quirks(adap, msgs, num))
  1973. return -EOPNOTSUPP;
  1974. /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
  1975. * enabled. This is an efficient way of keeping the for-loop from
  1976. * being executed when not needed.
  1977. */
  1978. if (static_key_false(&i2c_trace_msg)) {
  1979. int i;
  1980. for (i = 0; i < num; i++)
  1981. if (msgs[i].flags & I2C_M_RD)
  1982. trace_i2c_read(adap, &msgs[i], i);
  1983. else
  1984. trace_i2c_write(adap, &msgs[i], i);
  1985. }
  1986. /* Retry automatically on arbitration loss */
  1987. orig_jiffies = jiffies;
  1988. for (ret = 0, try = 0; try <= adap->retries; try++) {
  1989. ret = adap->algo->master_xfer(adap, msgs, num);
  1990. if (ret != -EAGAIN)
  1991. break;
  1992. if (time_after(jiffies, orig_jiffies + adap->timeout))
  1993. break;
  1994. }
  1995. if (static_key_false(&i2c_trace_msg)) {
  1996. int i;
  1997. for (i = 0; i < ret; i++)
  1998. if (msgs[i].flags & I2C_M_RD)
  1999. trace_i2c_reply(adap, &msgs[i], i);
  2000. trace_i2c_result(adap, i, ret);
  2001. }
  2002. return ret;
  2003. }
  2004. EXPORT_SYMBOL(__i2c_transfer);
  2005. /**
  2006. * i2c_transfer - execute a single or combined I2C message
  2007. * @adap: Handle to I2C bus
  2008. * @msgs: One or more messages to execute before STOP is issued to
  2009. * terminate the operation; each message begins with a START.
  2010. * @num: Number of messages to be executed.
  2011. *
  2012. * Returns negative errno, else the number of messages executed.
  2013. *
  2014. * Note that there is no requirement that each message be sent to
  2015. * the same slave address, although that is the most common model.
  2016. */
  2017. int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  2018. {
  2019. int ret;
  2020. /* REVISIT the fault reporting model here is weak:
  2021. *
  2022. * - When we get an error after receiving N bytes from a slave,
  2023. * there is no way to report "N".
  2024. *
  2025. * - When we get a NAK after transmitting N bytes to a slave,
  2026. * there is no way to report "N" ... or to let the master
  2027. * continue executing the rest of this combined message, if
  2028. * that's the appropriate response.
  2029. *
  2030. * - When for example "num" is two and we successfully complete
  2031. * the first message but get an error part way through the
  2032. * second, it's unclear whether that should be reported as
  2033. * one (discarding status on the second message) or errno
  2034. * (discarding status on the first one).
  2035. */
  2036. if (adap->algo->master_xfer) {
  2037. #ifdef DEBUG
  2038. for (ret = 0; ret < num; ret++) {
  2039. dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
  2040. "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
  2041. ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
  2042. (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
  2043. }
  2044. #endif
  2045. if (in_atomic() || irqs_disabled()) {
  2046. ret = adap->trylock_bus(adap, I2C_LOCK_SEGMENT);
  2047. if (!ret)
  2048. /* I2C activity is ongoing. */
  2049. return -EAGAIN;
  2050. } else {
  2051. i2c_lock_bus(adap, I2C_LOCK_SEGMENT);
  2052. }
  2053. ret = __i2c_transfer(adap, msgs, num);
  2054. i2c_unlock_bus(adap, I2C_LOCK_SEGMENT);
  2055. return ret;
  2056. } else {
  2057. dev_dbg(&adap->dev, "I2C level transfers not supported\n");
  2058. return -EOPNOTSUPP;
  2059. }
  2060. }
  2061. EXPORT_SYMBOL(i2c_transfer);
  2062. /**
  2063. * i2c_master_send - issue a single I2C message in master transmit mode
  2064. * @client: Handle to slave device
  2065. * @buf: Data that will be written to the slave
  2066. * @count: How many bytes to write, must be less than 64k since msg.len is u16
  2067. *
  2068. * Returns negative errno, or else the number of bytes written.
  2069. */
  2070. int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
  2071. {
  2072. int ret;
  2073. struct i2c_adapter *adap = client->adapter;
  2074. struct i2c_msg msg;
  2075. msg.addr = client->addr;
  2076. msg.flags = client->flags & I2C_M_TEN;
  2077. msg.len = count;
  2078. msg.buf = (char *)buf;
  2079. ret = i2c_transfer(adap, &msg, 1);
  2080. /*
  2081. * If everything went ok (i.e. 1 msg transmitted), return #bytes
  2082. * transmitted, else error code.
  2083. */
  2084. return (ret == 1) ? count : ret;
  2085. }
  2086. EXPORT_SYMBOL(i2c_master_send);
  2087. /**
  2088. * i2c_master_recv - issue a single I2C message in master receive mode
  2089. * @client: Handle to slave device
  2090. * @buf: Where to store data read from slave
  2091. * @count: How many bytes to read, must be less than 64k since msg.len is u16
  2092. *
  2093. * Returns negative errno, or else the number of bytes read.
  2094. */
  2095. int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
  2096. {
  2097. struct i2c_adapter *adap = client->adapter;
  2098. struct i2c_msg msg;
  2099. int ret;
  2100. msg.addr = client->addr;
  2101. msg.flags = client->flags & I2C_M_TEN;
  2102. msg.flags |= I2C_M_RD;
  2103. msg.len = count;
  2104. msg.buf = buf;
  2105. ret = i2c_transfer(adap, &msg, 1);
  2106. /*
  2107. * If everything went ok (i.e. 1 msg received), return #bytes received,
  2108. * else error code.
  2109. */
  2110. return (ret == 1) ? count : ret;
  2111. }
  2112. EXPORT_SYMBOL(i2c_master_recv);
  2113. /* ----------------------------------------------------
  2114. * the i2c address scanning function
  2115. * Will not work for 10-bit addresses!
  2116. * ----------------------------------------------------
  2117. */
  2118. /*
  2119. * Legacy default probe function, mostly relevant for SMBus. The default
  2120. * probe method is a quick write, but it is known to corrupt the 24RF08
  2121. * EEPROMs due to a state machine bug, and could also irreversibly
  2122. * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
  2123. * we use a short byte read instead. Also, some bus drivers don't implement
  2124. * quick write, so we fallback to a byte read in that case too.
  2125. * On x86, there is another special case for FSC hardware monitoring chips,
  2126. * which want regular byte reads (address 0x73.) Fortunately, these are the
  2127. * only known chips using this I2C address on PC hardware.
  2128. * Returns 1 if probe succeeded, 0 if not.
  2129. */
  2130. static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
  2131. {
  2132. int err;
  2133. union i2c_smbus_data dummy;
  2134. #ifdef CONFIG_X86
  2135. if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
  2136. && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
  2137. err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
  2138. I2C_SMBUS_BYTE_DATA, &dummy);
  2139. else
  2140. #endif
  2141. if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
  2142. && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
  2143. err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
  2144. I2C_SMBUS_QUICK, NULL);
  2145. else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
  2146. err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
  2147. I2C_SMBUS_BYTE, &dummy);
  2148. else {
  2149. dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
  2150. addr);
  2151. err = -EOPNOTSUPP;
  2152. }
  2153. return err >= 0;
  2154. }
  2155. static int i2c_detect_address(struct i2c_client *temp_client,
  2156. struct i2c_driver *driver)
  2157. {
  2158. struct i2c_board_info info;
  2159. struct i2c_adapter *adapter = temp_client->adapter;
  2160. int addr = temp_client->addr;
  2161. int err;
  2162. /* Make sure the address is valid */
  2163. err = i2c_check_7bit_addr_validity_strict(addr);
  2164. if (err) {
  2165. dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
  2166. addr);
  2167. return err;
  2168. }
  2169. /* Skip if already in use (7 bit, no need to encode flags) */
  2170. if (i2c_check_addr_busy(adapter, addr))
  2171. return 0;
  2172. /* Make sure there is something at this address */
  2173. if (!i2c_default_probe(adapter, addr))
  2174. return 0;
  2175. /* Finally call the custom detection function */
  2176. memset(&info, 0, sizeof(struct i2c_board_info));
  2177. info.addr = addr;
  2178. err = driver->detect(temp_client, &info);
  2179. if (err) {
  2180. /* -ENODEV is returned if the detection fails. We catch it
  2181. here as this isn't an error. */
  2182. return err == -ENODEV ? 0 : err;
  2183. }
  2184. /* Consistency check */
  2185. if (info.type[0] == '\0') {
  2186. dev_err(&adapter->dev, "%s detection function provided "
  2187. "no name for 0x%x\n", driver->driver.name,
  2188. addr);
  2189. } else {
  2190. struct i2c_client *client;
  2191. /* Detection succeeded, instantiate the device */
  2192. if (adapter->class & I2C_CLASS_DEPRECATED)
  2193. dev_warn(&adapter->dev,
  2194. "This adapter will soon drop class based instantiation of devices. "
  2195. "Please make sure client 0x%02x gets instantiated by other means. "
  2196. "Check 'Documentation/i2c/instantiating-devices' for details.\n",
  2197. info.addr);
  2198. dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
  2199. info.type, info.addr);
  2200. client = i2c_new_device(adapter, &info);
  2201. if (client)
  2202. list_add_tail(&client->detected, &driver->clients);
  2203. else
  2204. dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
  2205. info.type, info.addr);
  2206. }
  2207. return 0;
  2208. }
  2209. static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
  2210. {
  2211. const unsigned short *address_list;
  2212. struct i2c_client *temp_client;
  2213. int i, err = 0;
  2214. int adap_id = i2c_adapter_id(adapter);
  2215. address_list = driver->address_list;
  2216. if (!driver->detect || !address_list)
  2217. return 0;
  2218. /* Warn that the adapter lost class based instantiation */
  2219. if (adapter->class == I2C_CLASS_DEPRECATED) {
  2220. dev_dbg(&adapter->dev,
  2221. "This adapter dropped support for I2C classes and "
  2222. "won't auto-detect %s devices anymore. If you need it, check "
  2223. "'Documentation/i2c/instantiating-devices' for alternatives.\n",
  2224. driver->driver.name);
  2225. return 0;
  2226. }
  2227. /* Stop here if the classes do not match */
  2228. if (!(adapter->class & driver->class))
  2229. return 0;
  2230. /* Set up a temporary client to help detect callback */
  2231. temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
  2232. if (!temp_client)
  2233. return -ENOMEM;
  2234. temp_client->adapter = adapter;
  2235. for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
  2236. dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
  2237. "addr 0x%02x\n", adap_id, address_list[i]);
  2238. temp_client->addr = address_list[i];
  2239. err = i2c_detect_address(temp_client, driver);
  2240. if (unlikely(err))
  2241. break;
  2242. }
  2243. kfree(temp_client);
  2244. return err;
  2245. }
  2246. int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
  2247. {
  2248. return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
  2249. I2C_SMBUS_QUICK, NULL) >= 0;
  2250. }
  2251. EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
  2252. struct i2c_client *
  2253. i2c_new_probed_device(struct i2c_adapter *adap,
  2254. struct i2c_board_info *info,
  2255. unsigned short const *addr_list,
  2256. int (*probe)(struct i2c_adapter *, unsigned short addr))
  2257. {
  2258. int i;
  2259. if (!probe)
  2260. probe = i2c_default_probe;
  2261. for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
  2262. /* Check address validity */
  2263. if (i2c_check_7bit_addr_validity_strict(addr_list[i]) < 0) {
  2264. dev_warn(&adap->dev, "Invalid 7-bit address "
  2265. "0x%02x\n", addr_list[i]);
  2266. continue;
  2267. }
  2268. /* Check address availability (7 bit, no need to encode flags) */
  2269. if (i2c_check_addr_busy(adap, addr_list[i])) {
  2270. dev_dbg(&adap->dev, "Address 0x%02x already in "
  2271. "use, not probing\n", addr_list[i]);
  2272. continue;
  2273. }
  2274. /* Test address responsiveness */
  2275. if (probe(adap, addr_list[i]))
  2276. break;
  2277. }
  2278. if (addr_list[i] == I2C_CLIENT_END) {
  2279. dev_dbg(&adap->dev, "Probing failed, no device found\n");
  2280. return NULL;
  2281. }
  2282. info->addr = addr_list[i];
  2283. return i2c_new_device(adap, info);
  2284. }
  2285. EXPORT_SYMBOL_GPL(i2c_new_probed_device);
  2286. struct i2c_adapter *i2c_get_adapter(int nr)
  2287. {
  2288. struct i2c_adapter *adapter;
  2289. mutex_lock(&core_lock);
  2290. adapter = idr_find(&i2c_adapter_idr, nr);
  2291. if (!adapter)
  2292. goto exit;
  2293. if (try_module_get(adapter->owner))
  2294. get_device(&adapter->dev);
  2295. else
  2296. adapter = NULL;
  2297. exit:
  2298. mutex_unlock(&core_lock);
  2299. return adapter;
  2300. }
  2301. EXPORT_SYMBOL(i2c_get_adapter);
  2302. void i2c_put_adapter(struct i2c_adapter *adap)
  2303. {
  2304. if (!adap)
  2305. return;
  2306. put_device(&adap->dev);
  2307. module_put(adap->owner);
  2308. }
  2309. EXPORT_SYMBOL(i2c_put_adapter);
  2310. /* The SMBus parts */
  2311. #define POLY (0x1070U << 3)
  2312. static u8 crc8(u16 data)
  2313. {
  2314. int i;
  2315. for (i = 0; i < 8; i++) {
  2316. if (data & 0x8000)
  2317. data = data ^ POLY;
  2318. data = data << 1;
  2319. }
  2320. return (u8)(data >> 8);
  2321. }
  2322. /* Incremental CRC8 over count bytes in the array pointed to by p */
  2323. static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
  2324. {
  2325. int i;
  2326. for (i = 0; i < count; i++)
  2327. crc = crc8((crc ^ p[i]) << 8);
  2328. return crc;
  2329. }
  2330. /* Assume a 7-bit address, which is reasonable for SMBus */
  2331. static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
  2332. {
  2333. /* The address will be sent first */
  2334. u8 addr = i2c_8bit_addr_from_msg(msg);
  2335. pec = i2c_smbus_pec(pec, &addr, 1);
  2336. /* The data buffer follows */
  2337. return i2c_smbus_pec(pec, msg->buf, msg->len);
  2338. }
  2339. /* Used for write only transactions */
  2340. static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
  2341. {
  2342. msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
  2343. msg->len++;
  2344. }
  2345. /* Return <0 on CRC error
  2346. If there was a write before this read (most cases) we need to take the
  2347. partial CRC from the write part into account.
  2348. Note that this function does modify the message (we need to decrease the
  2349. message length to hide the CRC byte from the caller). */
  2350. static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
  2351. {
  2352. u8 rpec = msg->buf[--msg->len];
  2353. cpec = i2c_smbus_msg_pec(cpec, msg);
  2354. if (rpec != cpec) {
  2355. pr_debug("Bad PEC 0x%02x vs. 0x%02x\n",
  2356. rpec, cpec);
  2357. return -EBADMSG;
  2358. }
  2359. return 0;
  2360. }
  2361. /**
  2362. * i2c_smbus_read_byte - SMBus "receive byte" protocol
  2363. * @client: Handle to slave device
  2364. *
  2365. * This executes the SMBus "receive byte" protocol, returning negative errno
  2366. * else the byte received from the device.
  2367. */
  2368. s32 i2c_smbus_read_byte(const struct i2c_client *client)
  2369. {
  2370. union i2c_smbus_data data;
  2371. int status;
  2372. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  2373. I2C_SMBUS_READ, 0,
  2374. I2C_SMBUS_BYTE, &data);
  2375. return (status < 0) ? status : data.byte;
  2376. }
  2377. EXPORT_SYMBOL(i2c_smbus_read_byte);
  2378. /**
  2379. * i2c_smbus_write_byte - SMBus "send byte" protocol
  2380. * @client: Handle to slave device
  2381. * @value: Byte to be sent
  2382. *
  2383. * This executes the SMBus "send byte" protocol, returning negative errno
  2384. * else zero on success.
  2385. */
  2386. s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
  2387. {
  2388. return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  2389. I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
  2390. }
  2391. EXPORT_SYMBOL(i2c_smbus_write_byte);
  2392. /**
  2393. * i2c_smbus_read_byte_data - SMBus "read byte" protocol
  2394. * @client: Handle to slave device
  2395. * @command: Byte interpreted by slave
  2396. *
  2397. * This executes the SMBus "read byte" protocol, returning negative errno
  2398. * else a data byte received from the device.
  2399. */
  2400. s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
  2401. {
  2402. union i2c_smbus_data data;
  2403. int status;
  2404. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  2405. I2C_SMBUS_READ, command,
  2406. I2C_SMBUS_BYTE_DATA, &data);
  2407. return (status < 0) ? status : data.byte;
  2408. }
  2409. EXPORT_SYMBOL(i2c_smbus_read_byte_data);
  2410. /**
  2411. * i2c_smbus_write_byte_data - SMBus "write byte" protocol
  2412. * @client: Handle to slave device
  2413. * @command: Byte interpreted by slave
  2414. * @value: Byte being written
  2415. *
  2416. * This executes the SMBus "write byte" protocol, returning negative errno
  2417. * else zero on success.
  2418. */
  2419. s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
  2420. u8 value)
  2421. {
  2422. union i2c_smbus_data data;
  2423. data.byte = value;
  2424. return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  2425. I2C_SMBUS_WRITE, command,
  2426. I2C_SMBUS_BYTE_DATA, &data);
  2427. }
  2428. EXPORT_SYMBOL(i2c_smbus_write_byte_data);
  2429. /**
  2430. * i2c_smbus_read_word_data - SMBus "read word" protocol
  2431. * @client: Handle to slave device
  2432. * @command: Byte interpreted by slave
  2433. *
  2434. * This executes the SMBus "read word" protocol, returning negative errno
  2435. * else a 16-bit unsigned "word" received from the device.
  2436. */
  2437. s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
  2438. {
  2439. union i2c_smbus_data data;
  2440. int status;
  2441. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  2442. I2C_SMBUS_READ, command,
  2443. I2C_SMBUS_WORD_DATA, &data);
  2444. return (status < 0) ? status : data.word;
  2445. }
  2446. EXPORT_SYMBOL(i2c_smbus_read_word_data);
  2447. /**
  2448. * i2c_smbus_write_word_data - SMBus "write word" protocol
  2449. * @client: Handle to slave device
  2450. * @command: Byte interpreted by slave
  2451. * @value: 16-bit "word" being written
  2452. *
  2453. * This executes the SMBus "write word" protocol, returning negative errno
  2454. * else zero on success.
  2455. */
  2456. s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
  2457. u16 value)
  2458. {
  2459. union i2c_smbus_data data;
  2460. data.word = value;
  2461. return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  2462. I2C_SMBUS_WRITE, command,
  2463. I2C_SMBUS_WORD_DATA, &data);
  2464. }
  2465. EXPORT_SYMBOL(i2c_smbus_write_word_data);
  2466. /**
  2467. * i2c_smbus_read_block_data - SMBus "block read" protocol
  2468. * @client: Handle to slave device
  2469. * @command: Byte interpreted by slave
  2470. * @values: Byte array into which data will be read; big enough to hold
  2471. * the data returned by the slave. SMBus allows at most 32 bytes.
  2472. *
  2473. * This executes the SMBus "block read" protocol, returning negative errno
  2474. * else the number of data bytes in the slave's response.
  2475. *
  2476. * Note that using this function requires that the client's adapter support
  2477. * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
  2478. * support this; its emulation through I2C messaging relies on a specific
  2479. * mechanism (I2C_M_RECV_LEN) which may not be implemented.
  2480. */
  2481. s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
  2482. u8 *values)
  2483. {
  2484. union i2c_smbus_data data;
  2485. int status;
  2486. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  2487. I2C_SMBUS_READ, command,
  2488. I2C_SMBUS_BLOCK_DATA, &data);
  2489. if (status)
  2490. return status;
  2491. memcpy(values, &data.block[1], data.block[0]);
  2492. return data.block[0];
  2493. }
  2494. EXPORT_SYMBOL(i2c_smbus_read_block_data);
  2495. /**
  2496. * i2c_smbus_write_block_data - SMBus "block write" protocol
  2497. * @client: Handle to slave device
  2498. * @command: Byte interpreted by slave
  2499. * @length: Size of data block; SMBus allows at most 32 bytes
  2500. * @values: Byte array which will be written.
  2501. *
  2502. * This executes the SMBus "block write" protocol, returning negative errno
  2503. * else zero on success.
  2504. */
  2505. s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
  2506. u8 length, const u8 *values)
  2507. {
  2508. union i2c_smbus_data data;
  2509. if (length > I2C_SMBUS_BLOCK_MAX)
  2510. length = I2C_SMBUS_BLOCK_MAX;
  2511. data.block[0] = length;
  2512. memcpy(&data.block[1], values, length);
  2513. return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  2514. I2C_SMBUS_WRITE, command,
  2515. I2C_SMBUS_BLOCK_DATA, &data);
  2516. }
  2517. EXPORT_SYMBOL(i2c_smbus_write_block_data);
  2518. /* Returns the number of read bytes */
  2519. s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
  2520. u8 length, u8 *values)
  2521. {
  2522. union i2c_smbus_data data;
  2523. int status;
  2524. if (length > I2C_SMBUS_BLOCK_MAX)
  2525. length = I2C_SMBUS_BLOCK_MAX;
  2526. data.block[0] = length;
  2527. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  2528. I2C_SMBUS_READ, command,
  2529. I2C_SMBUS_I2C_BLOCK_DATA, &data);
  2530. if (status < 0)
  2531. return status;
  2532. memcpy(values, &data.block[1], data.block[0]);
  2533. return data.block[0];
  2534. }
  2535. EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
  2536. s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
  2537. u8 length, const u8 *values)
  2538. {
  2539. union i2c_smbus_data data;
  2540. if (length > I2C_SMBUS_BLOCK_MAX)
  2541. length = I2C_SMBUS_BLOCK_MAX;
  2542. data.block[0] = length;
  2543. memcpy(data.block + 1, values, length);
  2544. return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  2545. I2C_SMBUS_WRITE, command,
  2546. I2C_SMBUS_I2C_BLOCK_DATA, &data);
  2547. }
  2548. EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
  2549. /* Simulate a SMBus command using the i2c protocol
  2550. No checking of parameters is done! */
  2551. static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
  2552. unsigned short flags,
  2553. char read_write, u8 command, int size,
  2554. union i2c_smbus_data *data)
  2555. {
  2556. /* So we need to generate a series of msgs. In the case of writing, we
  2557. need to use only one message; when reading, we need two. We initialize
  2558. most things with sane defaults, to keep the code below somewhat
  2559. simpler. */
  2560. unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
  2561. unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
  2562. int num = read_write == I2C_SMBUS_READ ? 2 : 1;
  2563. int i;
  2564. u8 partial_pec = 0;
  2565. int status;
  2566. struct i2c_msg msg[2] = {
  2567. {
  2568. .addr = addr,
  2569. .flags = flags,
  2570. .len = 1,
  2571. .buf = msgbuf0,
  2572. }, {
  2573. .addr = addr,
  2574. .flags = flags | I2C_M_RD,
  2575. .len = 0,
  2576. .buf = msgbuf1,
  2577. },
  2578. };
  2579. msgbuf0[0] = command;
  2580. switch (size) {
  2581. case I2C_SMBUS_QUICK:
  2582. msg[0].len = 0;
  2583. /* Special case: The read/write field is used as data */
  2584. msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
  2585. I2C_M_RD : 0);
  2586. num = 1;
  2587. break;
  2588. case I2C_SMBUS_BYTE:
  2589. if (read_write == I2C_SMBUS_READ) {
  2590. /* Special case: only a read! */
  2591. msg[0].flags = I2C_M_RD | flags;
  2592. num = 1;
  2593. }
  2594. break;
  2595. case I2C_SMBUS_BYTE_DATA:
  2596. if (read_write == I2C_SMBUS_READ)
  2597. msg[1].len = 1;
  2598. else {
  2599. msg[0].len = 2;
  2600. msgbuf0[1] = data->byte;
  2601. }
  2602. break;
  2603. case I2C_SMBUS_WORD_DATA:
  2604. if (read_write == I2C_SMBUS_READ)
  2605. msg[1].len = 2;
  2606. else {
  2607. msg[0].len = 3;
  2608. msgbuf0[1] = data->word & 0xff;
  2609. msgbuf0[2] = data->word >> 8;
  2610. }
  2611. break;
  2612. case I2C_SMBUS_PROC_CALL:
  2613. num = 2; /* Special case */
  2614. read_write = I2C_SMBUS_READ;
  2615. msg[0].len = 3;
  2616. msg[1].len = 2;
  2617. msgbuf0[1] = data->word & 0xff;
  2618. msgbuf0[2] = data->word >> 8;
  2619. break;
  2620. case I2C_SMBUS_BLOCK_DATA:
  2621. if (read_write == I2C_SMBUS_READ) {
  2622. msg[1].flags |= I2C_M_RECV_LEN;
  2623. msg[1].len = 1; /* block length will be added by
  2624. the underlying bus driver */
  2625. } else {
  2626. msg[0].len = data->block[0] + 2;
  2627. if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
  2628. dev_err(&adapter->dev,
  2629. "Invalid block write size %d\n",
  2630. data->block[0]);
  2631. return -EINVAL;
  2632. }
  2633. for (i = 1; i < msg[0].len; i++)
  2634. msgbuf0[i] = data->block[i-1];
  2635. }
  2636. break;
  2637. case I2C_SMBUS_BLOCK_PROC_CALL:
  2638. num = 2; /* Another special case */
  2639. read_write = I2C_SMBUS_READ;
  2640. if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
  2641. dev_err(&adapter->dev,
  2642. "Invalid block write size %d\n",
  2643. data->block[0]);
  2644. return -EINVAL;
  2645. }
  2646. msg[0].len = data->block[0] + 2;
  2647. for (i = 1; i < msg[0].len; i++)
  2648. msgbuf0[i] = data->block[i-1];
  2649. msg[1].flags |= I2C_M_RECV_LEN;
  2650. msg[1].len = 1; /* block length will be added by
  2651. the underlying bus driver */
  2652. break;
  2653. case I2C_SMBUS_I2C_BLOCK_DATA:
  2654. if (read_write == I2C_SMBUS_READ) {
  2655. msg[1].len = data->block[0];
  2656. } else {
  2657. msg[0].len = data->block[0] + 1;
  2658. if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
  2659. dev_err(&adapter->dev,
  2660. "Invalid block write size %d\n",
  2661. data->block[0]);
  2662. return -EINVAL;
  2663. }
  2664. for (i = 1; i <= data->block[0]; i++)
  2665. msgbuf0[i] = data->block[i];
  2666. }
  2667. break;
  2668. default:
  2669. dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
  2670. return -EOPNOTSUPP;
  2671. }
  2672. i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
  2673. && size != I2C_SMBUS_I2C_BLOCK_DATA);
  2674. if (i) {
  2675. /* Compute PEC if first message is a write */
  2676. if (!(msg[0].flags & I2C_M_RD)) {
  2677. if (num == 1) /* Write only */
  2678. i2c_smbus_add_pec(&msg[0]);
  2679. else /* Write followed by read */
  2680. partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
  2681. }
  2682. /* Ask for PEC if last message is a read */
  2683. if (msg[num-1].flags & I2C_M_RD)
  2684. msg[num-1].len++;
  2685. }
  2686. status = i2c_transfer(adapter, msg, num);
  2687. if (status < 0)
  2688. return status;
  2689. /* Check PEC if last message is a read */
  2690. if (i && (msg[num-1].flags & I2C_M_RD)) {
  2691. status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
  2692. if (status < 0)
  2693. return status;
  2694. }
  2695. if (read_write == I2C_SMBUS_READ)
  2696. switch (size) {
  2697. case I2C_SMBUS_BYTE:
  2698. data->byte = msgbuf0[0];
  2699. break;
  2700. case I2C_SMBUS_BYTE_DATA:
  2701. data->byte = msgbuf1[0];
  2702. break;
  2703. case I2C_SMBUS_WORD_DATA:
  2704. case I2C_SMBUS_PROC_CALL:
  2705. data->word = msgbuf1[0] | (msgbuf1[1] << 8);
  2706. break;
  2707. case I2C_SMBUS_I2C_BLOCK_DATA:
  2708. for (i = 0; i < data->block[0]; i++)
  2709. data->block[i+1] = msgbuf1[i];
  2710. break;
  2711. case I2C_SMBUS_BLOCK_DATA:
  2712. case I2C_SMBUS_BLOCK_PROC_CALL:
  2713. for (i = 0; i < msgbuf1[0] + 1; i++)
  2714. data->block[i] = msgbuf1[i];
  2715. break;
  2716. }
  2717. return 0;
  2718. }
  2719. /**
  2720. * i2c_smbus_xfer - execute SMBus protocol operations
  2721. * @adapter: Handle to I2C bus
  2722. * @addr: Address of SMBus slave on that bus
  2723. * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
  2724. * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
  2725. * @command: Byte interpreted by slave, for protocols which use such bytes
  2726. * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
  2727. * @data: Data to be read or written
  2728. *
  2729. * This executes an SMBus protocol operation, and returns a negative
  2730. * errno code else zero on success.
  2731. */
  2732. s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
  2733. char read_write, u8 command, int protocol,
  2734. union i2c_smbus_data *data)
  2735. {
  2736. unsigned long orig_jiffies;
  2737. int try;
  2738. s32 res;
  2739. /* If enabled, the following two tracepoints are conditional on
  2740. * read_write and protocol.
  2741. */
  2742. trace_smbus_write(adapter, addr, flags, read_write,
  2743. command, protocol, data);
  2744. trace_smbus_read(adapter, addr, flags, read_write,
  2745. command, protocol);
  2746. flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
  2747. if (adapter->algo->smbus_xfer) {
  2748. i2c_lock_bus(adapter, I2C_LOCK_SEGMENT);
  2749. /* Retry automatically on arbitration loss */
  2750. orig_jiffies = jiffies;
  2751. for (res = 0, try = 0; try <= adapter->retries; try++) {
  2752. res = adapter->algo->smbus_xfer(adapter, addr, flags,
  2753. read_write, command,
  2754. protocol, data);
  2755. if (res != -EAGAIN)
  2756. break;
  2757. if (time_after(jiffies,
  2758. orig_jiffies + adapter->timeout))
  2759. break;
  2760. }
  2761. i2c_unlock_bus(adapter, I2C_LOCK_SEGMENT);
  2762. if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
  2763. goto trace;
  2764. /*
  2765. * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
  2766. * implement native support for the SMBus operation.
  2767. */
  2768. }
  2769. res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
  2770. command, protocol, data);
  2771. trace:
  2772. /* If enabled, the reply tracepoint is conditional on read_write. */
  2773. trace_smbus_reply(adapter, addr, flags, read_write,
  2774. command, protocol, data);
  2775. trace_smbus_result(adapter, addr, flags, read_write,
  2776. command, protocol, res);
  2777. return res;
  2778. }
  2779. EXPORT_SYMBOL(i2c_smbus_xfer);
  2780. /**
  2781. * i2c_smbus_read_i2c_block_data_or_emulated - read block or emulate
  2782. * @client: Handle to slave device
  2783. * @command: Byte interpreted by slave
  2784. * @length: Size of data block; SMBus allows at most I2C_SMBUS_BLOCK_MAX bytes
  2785. * @values: Byte array into which data will be read; big enough to hold
  2786. * the data returned by the slave. SMBus allows at most
  2787. * I2C_SMBUS_BLOCK_MAX bytes.
  2788. *
  2789. * This executes the SMBus "block read" protocol if supported by the adapter.
  2790. * If block read is not supported, it emulates it using either word or byte
  2791. * read protocols depending on availability.
  2792. *
  2793. * The addresses of the I2C slave device that are accessed with this function
  2794. * must be mapped to a linear region, so that a block read will have the same
  2795. * effect as a byte read. Before using this function you must double-check
  2796. * if the I2C slave does support exchanging a block transfer with a byte
  2797. * transfer.
  2798. */
  2799. s32 i2c_smbus_read_i2c_block_data_or_emulated(const struct i2c_client *client,
  2800. u8 command, u8 length, u8 *values)
  2801. {
  2802. u8 i = 0;
  2803. int status;
  2804. if (length > I2C_SMBUS_BLOCK_MAX)
  2805. length = I2C_SMBUS_BLOCK_MAX;
  2806. if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK))
  2807. return i2c_smbus_read_i2c_block_data(client, command, length, values);
  2808. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA))
  2809. return -EOPNOTSUPP;
  2810. if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_WORD_DATA)) {
  2811. while ((i + 2) <= length) {
  2812. status = i2c_smbus_read_word_data(client, command + i);
  2813. if (status < 0)
  2814. return status;
  2815. values[i] = status & 0xff;
  2816. values[i + 1] = status >> 8;
  2817. i += 2;
  2818. }
  2819. }
  2820. while (i < length) {
  2821. status = i2c_smbus_read_byte_data(client, command + i);
  2822. if (status < 0)
  2823. return status;
  2824. values[i] = status;
  2825. i++;
  2826. }
  2827. return i;
  2828. }
  2829. EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data_or_emulated);
  2830. #if IS_ENABLED(CONFIG_I2C_SLAVE)
  2831. int i2c_slave_register(struct i2c_client *client, i2c_slave_cb_t slave_cb)
  2832. {
  2833. int ret;
  2834. if (!client || !slave_cb) {
  2835. WARN(1, "insufficent data\n");
  2836. return -EINVAL;
  2837. }
  2838. if (!(client->flags & I2C_CLIENT_SLAVE))
  2839. dev_warn(&client->dev, "%s: client slave flag not set. You might see address collisions\n",
  2840. __func__);
  2841. if (!(client->flags & I2C_CLIENT_TEN)) {
  2842. /* Enforce stricter address checking */
  2843. ret = i2c_check_7bit_addr_validity_strict(client->addr);
  2844. if (ret) {
  2845. dev_err(&client->dev, "%s: invalid address\n", __func__);
  2846. return ret;
  2847. }
  2848. }
  2849. if (!client->adapter->algo->reg_slave) {
  2850. dev_err(&client->dev, "%s: not supported by adapter\n", __func__);
  2851. return -EOPNOTSUPP;
  2852. }
  2853. client->slave_cb = slave_cb;
  2854. i2c_lock_adapter(client->adapter);
  2855. ret = client->adapter->algo->reg_slave(client);
  2856. i2c_unlock_adapter(client->adapter);
  2857. if (ret) {
  2858. client->slave_cb = NULL;
  2859. dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret);
  2860. }
  2861. return ret;
  2862. }
  2863. EXPORT_SYMBOL_GPL(i2c_slave_register);
  2864. int i2c_slave_unregister(struct i2c_client *client)
  2865. {
  2866. int ret;
  2867. if (!client->adapter->algo->unreg_slave) {
  2868. dev_err(&client->dev, "%s: not supported by adapter\n", __func__);
  2869. return -EOPNOTSUPP;
  2870. }
  2871. i2c_lock_adapter(client->adapter);
  2872. ret = client->adapter->algo->unreg_slave(client);
  2873. i2c_unlock_adapter(client->adapter);
  2874. if (ret == 0)
  2875. client->slave_cb = NULL;
  2876. else
  2877. dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret);
  2878. return ret;
  2879. }
  2880. EXPORT_SYMBOL_GPL(i2c_slave_unregister);
  2881. #endif
  2882. MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
  2883. MODULE_DESCRIPTION("I2C-Bus main module");
  2884. MODULE_LICENSE("GPL");