saa7164-i2c.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Driver for the NXP SAA7164 PCIe bridge
  3. *
  4. * Copyright (c) 2010-2015 Steven Toth <stoth@kernellabs.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. *
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <linux/module.h>
  22. #include <linux/moduleparam.h>
  23. #include <linux/init.h>
  24. #include <linux/delay.h>
  25. #include <linux/io.h>
  26. #include "saa7164.h"
  27. static int i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
  28. {
  29. struct saa7164_i2c *bus = i2c_adap->algo_data;
  30. struct saa7164_dev *dev = bus->dev;
  31. int i, retval = 0;
  32. dprintk(DBGLVL_I2C, "%s(num = %d)\n", __func__, num);
  33. for (i = 0 ; i < num; i++) {
  34. dprintk(DBGLVL_I2C, "%s(num = %d) addr = 0x%02x len = 0x%x\n",
  35. __func__, num, msgs[i].addr, msgs[i].len);
  36. if (msgs[i].flags & I2C_M_RD) {
  37. retval = saa7164_api_i2c_read(bus,
  38. msgs[i].addr,
  39. 0 /* reglen */,
  40. NULL /* reg */, msgs[i].len, msgs[i].buf);
  41. } else if (i + 1 < num && (msgs[i + 1].flags & I2C_M_RD) &&
  42. msgs[i].addr == msgs[i + 1].addr) {
  43. /* write then read from same address */
  44. retval = saa7164_api_i2c_read(bus, msgs[i].addr,
  45. msgs[i].len, msgs[i].buf,
  46. msgs[i+1].len, msgs[i+1].buf
  47. );
  48. i++;
  49. if (retval < 0)
  50. goto err;
  51. } else {
  52. /* write */
  53. retval = saa7164_api_i2c_write(bus, msgs[i].addr,
  54. msgs[i].len, msgs[i].buf);
  55. }
  56. if (retval < 0)
  57. goto err;
  58. }
  59. return num;
  60. err:
  61. return retval;
  62. }
  63. static u32 saa7164_functionality(struct i2c_adapter *adap)
  64. {
  65. return I2C_FUNC_I2C;
  66. }
  67. static struct i2c_algorithm saa7164_i2c_algo_template = {
  68. .master_xfer = i2c_xfer,
  69. .functionality = saa7164_functionality,
  70. };
  71. /* ----------------------------------------------------------------------- */
  72. static struct i2c_adapter saa7164_i2c_adap_template = {
  73. .name = "saa7164",
  74. .owner = THIS_MODULE,
  75. .algo = &saa7164_i2c_algo_template,
  76. };
  77. static struct i2c_client saa7164_i2c_client_template = {
  78. .name = "saa7164 internal",
  79. };
  80. int saa7164_i2c_register(struct saa7164_i2c *bus)
  81. {
  82. struct saa7164_dev *dev = bus->dev;
  83. dprintk(DBGLVL_I2C, "%s(bus = %d)\n", __func__, bus->nr);
  84. bus->i2c_adap = saa7164_i2c_adap_template;
  85. bus->i2c_client = saa7164_i2c_client_template;
  86. bus->i2c_adap.dev.parent = &dev->pci->dev;
  87. strlcpy(bus->i2c_adap.name, bus->dev->name,
  88. sizeof(bus->i2c_adap.name));
  89. bus->i2c_adap.algo_data = bus;
  90. i2c_set_adapdata(&bus->i2c_adap, bus);
  91. i2c_add_adapter(&bus->i2c_adap);
  92. bus->i2c_client.adapter = &bus->i2c_adap;
  93. if (0 != bus->i2c_rc)
  94. printk(KERN_ERR "%s: i2c bus %d register FAILED\n",
  95. dev->name, bus->nr);
  96. return bus->i2c_rc;
  97. }
  98. int saa7164_i2c_unregister(struct saa7164_i2c *bus)
  99. {
  100. i2c_del_adapter(&bus->i2c_adap);
  101. return 0;
  102. }