v4l2-i2c.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * v4l2-i2c - I2C helpers for Video4Linux2
  4. */
  5. #include <linux/i2c.h>
  6. #include <linux/module.h>
  7. #include <media/v4l2-common.h>
  8. #include <media/v4l2-device.h>
  9. void v4l2_i2c_subdev_unregister(struct v4l2_subdev *sd)
  10. {
  11. struct i2c_client *client = v4l2_get_subdevdata(sd);
  12. /*
  13. * We need to unregister the i2c client
  14. * explicitly. We cannot rely on
  15. * i2c_del_adapter to always unregister
  16. * clients for us, since if the i2c bus is a
  17. * platform bus, then it is never deleted.
  18. *
  19. * Device tree or ACPI based devices must not
  20. * be unregistered as they have not been
  21. * registered by us, and would not be
  22. * re-created by just probing the V4L2 driver.
  23. */
  24. if (client && !client->dev.of_node && !client->dev.fwnode)
  25. i2c_unregister_device(client);
  26. }
  27. void v4l2_i2c_subdev_set_name(struct v4l2_subdev *sd,
  28. struct i2c_client *client,
  29. const char *devname, const char *postfix)
  30. {
  31. if (!devname)
  32. devname = client->dev.driver->name;
  33. if (!postfix)
  34. postfix = "";
  35. snprintf(sd->name, sizeof(sd->name), "%s%s %d-%04x", devname, postfix,
  36. i2c_adapter_id(client->adapter), client->addr);
  37. }
  38. EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_set_name);
  39. void v4l2_i2c_subdev_init(struct v4l2_subdev *sd, struct i2c_client *client,
  40. const struct v4l2_subdev_ops *ops)
  41. {
  42. v4l2_subdev_init(sd, ops);
  43. sd->flags |= V4L2_SUBDEV_FL_IS_I2C;
  44. /* the owner is the same as the i2c_client's driver owner */
  45. sd->owner = client->dev.driver->owner;
  46. sd->dev = &client->dev;
  47. /* i2c_client and v4l2_subdev point to one another */
  48. v4l2_set_subdevdata(sd, client);
  49. i2c_set_clientdata(client, sd);
  50. v4l2_i2c_subdev_set_name(sd, client, NULL, NULL);
  51. }
  52. EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_init);
  53. /* Load an i2c sub-device. */
  54. struct v4l2_subdev
  55. *v4l2_i2c_new_subdev_board(struct v4l2_device *v4l2_dev,
  56. struct i2c_adapter *adapter,
  57. struct i2c_board_info *info,
  58. const unsigned short *probe_addrs)
  59. {
  60. struct v4l2_subdev *sd = NULL;
  61. struct i2c_client *client;
  62. if (!v4l2_dev)
  63. return NULL;
  64. request_module(I2C_MODULE_PREFIX "%s", info->type);
  65. /* Create the i2c client */
  66. if (info->addr == 0 && probe_addrs)
  67. client = i2c_new_scanned_device(adapter, info, probe_addrs,
  68. NULL);
  69. else
  70. client = i2c_new_client_device(adapter, info);
  71. /*
  72. * Note: by loading the module first we are certain that c->driver
  73. * will be set if the driver was found. If the module was not loaded
  74. * first, then the i2c core tries to delay-load the module for us,
  75. * and then c->driver is still NULL until the module is finally
  76. * loaded. This delay-load mechanism doesn't work if other drivers
  77. * want to use the i2c device, so explicitly loading the module
  78. * is the best alternative.
  79. */
  80. if (!i2c_client_has_driver(client))
  81. goto error;
  82. /* Lock the module so we can safely get the v4l2_subdev pointer */
  83. if (!try_module_get(client->dev.driver->owner))
  84. goto error;
  85. sd = i2c_get_clientdata(client);
  86. /*
  87. * Register with the v4l2_device which increases the module's
  88. * use count as well.
  89. */
  90. if (__v4l2_device_register_subdev(v4l2_dev, sd, sd->owner))
  91. sd = NULL;
  92. /* Decrease the module use count to match the first try_module_get. */
  93. module_put(client->dev.driver->owner);
  94. error:
  95. /*
  96. * If we have a client but no subdev, then something went wrong and
  97. * we must unregister the client.
  98. */
  99. if (!IS_ERR(client) && !sd)
  100. i2c_unregister_device(client);
  101. return sd;
  102. }
  103. EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev_board);
  104. struct v4l2_subdev *v4l2_i2c_new_subdev(struct v4l2_device *v4l2_dev,
  105. struct i2c_adapter *adapter,
  106. const char *client_type,
  107. u8 addr,
  108. const unsigned short *probe_addrs)
  109. {
  110. struct i2c_board_info info;
  111. /*
  112. * Setup the i2c board info with the device type and
  113. * the device address.
  114. */
  115. memset(&info, 0, sizeof(info));
  116. strscpy(info.type, client_type, sizeof(info.type));
  117. info.addr = addr;
  118. return v4l2_i2c_new_subdev_board(v4l2_dev, adapter, &info,
  119. probe_addrs);
  120. }
  121. EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev);
  122. /* Return i2c client address of v4l2_subdev. */
  123. unsigned short v4l2_i2c_subdev_addr(struct v4l2_subdev *sd)
  124. {
  125. struct i2c_client *client = v4l2_get_subdevdata(sd);
  126. return client ? client->addr : I2C_CLIENT_END;
  127. }
  128. EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_addr);
  129. /*
  130. * Return a list of I2C tuner addresses to probe. Use only if the tuner
  131. * addresses are unknown.
  132. */
  133. const unsigned short *v4l2_i2c_tuner_addrs(enum v4l2_i2c_tuner_type type)
  134. {
  135. static const unsigned short radio_addrs[] = {
  136. #if IS_ENABLED(CONFIG_MEDIA_TUNER_TEA5761)
  137. 0x10,
  138. #endif
  139. 0x60,
  140. I2C_CLIENT_END
  141. };
  142. static const unsigned short demod_addrs[] = {
  143. 0x42, 0x43, 0x4a, 0x4b,
  144. I2C_CLIENT_END
  145. };
  146. static const unsigned short tv_addrs[] = {
  147. 0x42, 0x43, 0x4a, 0x4b, /* tda8290 */
  148. 0x60, 0x61, 0x62, 0x63, 0x64,
  149. I2C_CLIENT_END
  150. };
  151. switch (type) {
  152. case ADDRS_RADIO:
  153. return radio_addrs;
  154. case ADDRS_DEMOD:
  155. return demod_addrs;
  156. case ADDRS_TV:
  157. return tv_addrs;
  158. case ADDRS_TV_WITH_DEMOD:
  159. return tv_addrs + 4;
  160. }
  161. return NULL;
  162. }
  163. EXPORT_SYMBOL_GPL(v4l2_i2c_tuner_addrs);