i2c-cbus-gpio.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * CBUS I2C driver for Nokia Internet Tablets.
  3. *
  4. * Copyright (C) 2004-2010 Nokia Corporation
  5. *
  6. * Based on code written by Juha Yrjölä, David Weinehall, Mikko Ylinen and
  7. * Felipe Balbi. Converted to I2C driver by Aaro Koskinen.
  8. *
  9. * This file is subject to the terms and conditions of the GNU General
  10. * Public License. See the file "COPYING" in the main directory of this
  11. * archive for more details.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/io.h>
  19. #include <linux/i2c.h>
  20. #include <linux/slab.h>
  21. #include <linux/delay.h>
  22. #include <linux/errno.h>
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/gpio/consumer.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/platform_device.h>
  28. /*
  29. * Bit counts are derived from Nokia implementation. These should be checked
  30. * if other CBUS implementations appear.
  31. */
  32. #define CBUS_ADDR_BITS 3
  33. #define CBUS_REG_BITS 5
  34. struct cbus_host {
  35. spinlock_t lock; /* host lock */
  36. struct device *dev;
  37. struct gpio_desc *clk;
  38. struct gpio_desc *dat;
  39. struct gpio_desc *sel;
  40. };
  41. /**
  42. * cbus_send_bit - sends one bit over the bus
  43. * @host: the host we're using
  44. * @bit: one bit of information to send
  45. */
  46. static void cbus_send_bit(struct cbus_host *host, unsigned bit)
  47. {
  48. gpiod_set_value(host->dat, bit ? 1 : 0);
  49. gpiod_set_value(host->clk, 1);
  50. gpiod_set_value(host->clk, 0);
  51. }
  52. /**
  53. * cbus_send_data - sends @len amount of data over the bus
  54. * @host: the host we're using
  55. * @data: the data to send
  56. * @len: size of the transfer
  57. */
  58. static void cbus_send_data(struct cbus_host *host, unsigned data, unsigned len)
  59. {
  60. int i;
  61. for (i = len; i > 0; i--)
  62. cbus_send_bit(host, data & (1 << (i - 1)));
  63. }
  64. /**
  65. * cbus_receive_bit - receives one bit from the bus
  66. * @host: the host we're using
  67. */
  68. static int cbus_receive_bit(struct cbus_host *host)
  69. {
  70. int ret;
  71. gpiod_set_value(host->clk, 1);
  72. ret = gpiod_get_value(host->dat);
  73. gpiod_set_value(host->clk, 0);
  74. return ret;
  75. }
  76. /**
  77. * cbus_receive_word - receives 16-bit word from the bus
  78. * @host: the host we're using
  79. */
  80. static int cbus_receive_word(struct cbus_host *host)
  81. {
  82. int ret = 0;
  83. int i;
  84. for (i = 16; i > 0; i--) {
  85. int bit = cbus_receive_bit(host);
  86. if (bit < 0)
  87. return bit;
  88. if (bit)
  89. ret |= 1 << (i - 1);
  90. }
  91. return ret;
  92. }
  93. /**
  94. * cbus_transfer - transfers data over the bus
  95. * @host: the host we're using
  96. * @rw: read/write flag
  97. * @dev: device address
  98. * @reg: register address
  99. * @data: if @rw == I2C_SBUS_WRITE data to send otherwise 0
  100. */
  101. static int cbus_transfer(struct cbus_host *host, char rw, unsigned dev,
  102. unsigned reg, unsigned data)
  103. {
  104. unsigned long flags;
  105. int ret;
  106. /* We don't want interrupts disturbing our transfer */
  107. spin_lock_irqsave(&host->lock, flags);
  108. /* Reset state and start of transfer, SEL stays down during transfer */
  109. gpiod_set_value(host->sel, 0);
  110. /* Set the DAT pin to output */
  111. gpiod_direction_output(host->dat, 1);
  112. /* Send the device address */
  113. cbus_send_data(host, dev, CBUS_ADDR_BITS);
  114. /* Send the rw flag */
  115. cbus_send_bit(host, rw == I2C_SMBUS_READ);
  116. /* Send the register address */
  117. cbus_send_data(host, reg, CBUS_REG_BITS);
  118. if (rw == I2C_SMBUS_WRITE) {
  119. cbus_send_data(host, data, 16);
  120. ret = 0;
  121. } else {
  122. ret = gpiod_direction_input(host->dat);
  123. if (ret) {
  124. dev_dbg(host->dev, "failed setting direction\n");
  125. goto out;
  126. }
  127. gpiod_set_value(host->clk, 1);
  128. ret = cbus_receive_word(host);
  129. if (ret < 0) {
  130. dev_dbg(host->dev, "failed receiving data\n");
  131. goto out;
  132. }
  133. }
  134. /* Indicate end of transfer, SEL goes up until next transfer */
  135. gpiod_set_value(host->sel, 1);
  136. gpiod_set_value(host->clk, 1);
  137. gpiod_set_value(host->clk, 0);
  138. out:
  139. spin_unlock_irqrestore(&host->lock, flags);
  140. return ret;
  141. }
  142. static int cbus_i2c_smbus_xfer(struct i2c_adapter *adapter,
  143. u16 addr,
  144. unsigned short flags,
  145. char read_write,
  146. u8 command,
  147. int size,
  148. union i2c_smbus_data *data)
  149. {
  150. struct cbus_host *chost = i2c_get_adapdata(adapter);
  151. int ret;
  152. if (size != I2C_SMBUS_WORD_DATA)
  153. return -EINVAL;
  154. ret = cbus_transfer(chost, read_write == I2C_SMBUS_READ, addr,
  155. command, data->word);
  156. if (ret < 0)
  157. return ret;
  158. if (read_write == I2C_SMBUS_READ)
  159. data->word = ret;
  160. return 0;
  161. }
  162. static u32 cbus_i2c_func(struct i2c_adapter *adapter)
  163. {
  164. return I2C_FUNC_SMBUS_READ_WORD_DATA | I2C_FUNC_SMBUS_WRITE_WORD_DATA;
  165. }
  166. static const struct i2c_algorithm cbus_i2c_algo = {
  167. .smbus_xfer = cbus_i2c_smbus_xfer,
  168. .smbus_xfer_atomic = cbus_i2c_smbus_xfer,
  169. .functionality = cbus_i2c_func,
  170. };
  171. static int cbus_i2c_remove(struct platform_device *pdev)
  172. {
  173. struct i2c_adapter *adapter = platform_get_drvdata(pdev);
  174. i2c_del_adapter(adapter);
  175. return 0;
  176. }
  177. static int cbus_i2c_probe(struct platform_device *pdev)
  178. {
  179. struct i2c_adapter *adapter;
  180. struct cbus_host *chost;
  181. adapter = devm_kzalloc(&pdev->dev, sizeof(struct i2c_adapter),
  182. GFP_KERNEL);
  183. if (!adapter)
  184. return -ENOMEM;
  185. chost = devm_kzalloc(&pdev->dev, sizeof(*chost), GFP_KERNEL);
  186. if (!chost)
  187. return -ENOMEM;
  188. if (gpiod_count(&pdev->dev, NULL) != 3)
  189. return -ENODEV;
  190. chost->clk = devm_gpiod_get_index(&pdev->dev, NULL, 0, GPIOD_OUT_LOW);
  191. if (IS_ERR(chost->clk))
  192. return PTR_ERR(chost->clk);
  193. chost->dat = devm_gpiod_get_index(&pdev->dev, NULL, 1, GPIOD_IN);
  194. if (IS_ERR(chost->dat))
  195. return PTR_ERR(chost->dat);
  196. chost->sel = devm_gpiod_get_index(&pdev->dev, NULL, 2, GPIOD_OUT_HIGH);
  197. if (IS_ERR(chost->sel))
  198. return PTR_ERR(chost->sel);
  199. gpiod_set_consumer_name(chost->clk, "CBUS clk");
  200. gpiod_set_consumer_name(chost->dat, "CBUS dat");
  201. gpiod_set_consumer_name(chost->sel, "CBUS sel");
  202. adapter->owner = THIS_MODULE;
  203. adapter->class = I2C_CLASS_HWMON;
  204. adapter->dev.parent = &pdev->dev;
  205. adapter->dev.of_node = pdev->dev.of_node;
  206. adapter->nr = pdev->id;
  207. adapter->timeout = HZ;
  208. adapter->algo = &cbus_i2c_algo;
  209. strscpy(adapter->name, "CBUS I2C adapter", sizeof(adapter->name));
  210. spin_lock_init(&chost->lock);
  211. chost->dev = &pdev->dev;
  212. i2c_set_adapdata(adapter, chost);
  213. platform_set_drvdata(pdev, adapter);
  214. return i2c_add_numbered_adapter(adapter);
  215. }
  216. #if defined(CONFIG_OF)
  217. static const struct of_device_id i2c_cbus_dt_ids[] = {
  218. { .compatible = "i2c-cbus-gpio", },
  219. { }
  220. };
  221. MODULE_DEVICE_TABLE(of, i2c_cbus_dt_ids);
  222. #endif
  223. static struct platform_driver cbus_i2c_driver = {
  224. .probe = cbus_i2c_probe,
  225. .remove = cbus_i2c_remove,
  226. .driver = {
  227. .name = "i2c-cbus-gpio",
  228. .of_match_table = of_match_ptr(i2c_cbus_dt_ids),
  229. },
  230. };
  231. module_platform_driver(cbus_i2c_driver);
  232. MODULE_ALIAS("platform:i2c-cbus-gpio");
  233. MODULE_DESCRIPTION("CBUS I2C driver");
  234. MODULE_AUTHOR("Juha Yrjölä");
  235. MODULE_AUTHOR("David Weinehall");
  236. MODULE_AUTHOR("Mikko Ylinen");
  237. MODULE_AUTHOR("Felipe Balbi");
  238. MODULE_AUTHOR("Aaro Koskinen <[email protected]>");
  239. MODULE_LICENSE("GPL");