saa7164-i2c.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Driver for the NXP SAA7164 PCIe bridge
  4. *
  5. * Copyright (c) 2010-2015 Steven Toth <[email protected]>
  6. */
  7. #include <linux/module.h>
  8. #include <linux/moduleparam.h>
  9. #include <linux/init.h>
  10. #include <linux/delay.h>
  11. #include <linux/io.h>
  12. #include "saa7164.h"
  13. static int i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
  14. {
  15. struct saa7164_i2c *bus = i2c_adap->algo_data;
  16. struct saa7164_dev *dev = bus->dev;
  17. int i, retval = 0;
  18. dprintk(DBGLVL_I2C, "%s(num = %d)\n", __func__, num);
  19. for (i = 0 ; i < num; i++) {
  20. dprintk(DBGLVL_I2C, "%s(num = %d) addr = 0x%02x len = 0x%x\n",
  21. __func__, num, msgs[i].addr, msgs[i].len);
  22. if (msgs[i].flags & I2C_M_RD) {
  23. retval = saa7164_api_i2c_read(bus,
  24. msgs[i].addr,
  25. 0 /* reglen */,
  26. NULL /* reg */, msgs[i].len, msgs[i].buf);
  27. } else if (i + 1 < num && (msgs[i + 1].flags & I2C_M_RD) &&
  28. msgs[i].addr == msgs[i + 1].addr) {
  29. /* write then read from same address */
  30. retval = saa7164_api_i2c_read(bus, msgs[i].addr,
  31. msgs[i].len, msgs[i].buf,
  32. msgs[i+1].len, msgs[i+1].buf
  33. );
  34. i++;
  35. if (retval < 0)
  36. goto err;
  37. } else {
  38. /* write */
  39. retval = saa7164_api_i2c_write(bus, msgs[i].addr,
  40. msgs[i].len, msgs[i].buf);
  41. }
  42. if (retval < 0)
  43. goto err;
  44. }
  45. return num;
  46. err:
  47. return retval;
  48. }
  49. static u32 saa7164_functionality(struct i2c_adapter *adap)
  50. {
  51. return I2C_FUNC_I2C;
  52. }
  53. static const struct i2c_algorithm saa7164_i2c_algo_template = {
  54. .master_xfer = i2c_xfer,
  55. .functionality = saa7164_functionality,
  56. };
  57. /* ----------------------------------------------------------------------- */
  58. static const struct i2c_adapter saa7164_i2c_adap_template = {
  59. .name = "saa7164",
  60. .owner = THIS_MODULE,
  61. .algo = &saa7164_i2c_algo_template,
  62. };
  63. static const struct i2c_client saa7164_i2c_client_template = {
  64. .name = "saa7164 internal",
  65. };
  66. int saa7164_i2c_register(struct saa7164_i2c *bus)
  67. {
  68. struct saa7164_dev *dev = bus->dev;
  69. dprintk(DBGLVL_I2C, "%s(bus = %d)\n", __func__, bus->nr);
  70. bus->i2c_adap = saa7164_i2c_adap_template;
  71. bus->i2c_client = saa7164_i2c_client_template;
  72. bus->i2c_adap.dev.parent = &dev->pci->dev;
  73. strscpy(bus->i2c_adap.name, bus->dev->name,
  74. sizeof(bus->i2c_adap.name));
  75. bus->i2c_adap.algo_data = bus;
  76. i2c_set_adapdata(&bus->i2c_adap, bus);
  77. i2c_add_adapter(&bus->i2c_adap);
  78. bus->i2c_client.adapter = &bus->i2c_adap;
  79. if (0 != bus->i2c_rc)
  80. printk(KERN_ERR "%s: i2c bus %d register FAILED\n",
  81. dev->name, bus->nr);
  82. return bus->i2c_rc;
  83. }
  84. int saa7164_i2c_unregister(struct saa7164_i2c *bus)
  85. {
  86. i2c_del_adapter(&bus->i2c_adap);
  87. return 0;
  88. }