i2c-cbus-gpio.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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/gpio.h>
  21. #include <linux/slab.h>
  22. #include <linux/delay.h>
  23. #include <linux/errno.h>
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/of_gpio.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/platform_data/i2c-cbus-gpio.h>
  30. /*
  31. * Bit counts are derived from Nokia implementation. These should be checked
  32. * if other CBUS implementations appear.
  33. */
  34. #define CBUS_ADDR_BITS 3
  35. #define CBUS_REG_BITS 5
  36. struct cbus_host {
  37. spinlock_t lock; /* host lock */
  38. struct device *dev;
  39. int clk_gpio;
  40. int dat_gpio;
  41. int sel_gpio;
  42. };
  43. /**
  44. * cbus_send_bit - sends one bit over the bus
  45. * @host: the host we're using
  46. * @bit: one bit of information to send
  47. */
  48. static void cbus_send_bit(struct cbus_host *host, unsigned bit)
  49. {
  50. gpio_set_value(host->dat_gpio, bit ? 1 : 0);
  51. gpio_set_value(host->clk_gpio, 1);
  52. gpio_set_value(host->clk_gpio, 0);
  53. }
  54. /**
  55. * cbus_send_data - sends @len amount of data over the bus
  56. * @host: the host we're using
  57. * @data: the data to send
  58. * @len: size of the transfer
  59. */
  60. static void cbus_send_data(struct cbus_host *host, unsigned data, unsigned len)
  61. {
  62. int i;
  63. for (i = len; i > 0; i--)
  64. cbus_send_bit(host, data & (1 << (i - 1)));
  65. }
  66. /**
  67. * cbus_receive_bit - receives one bit from the bus
  68. * @host: the host we're using
  69. */
  70. static int cbus_receive_bit(struct cbus_host *host)
  71. {
  72. int ret;
  73. gpio_set_value(host->clk_gpio, 1);
  74. ret = gpio_get_value(host->dat_gpio);
  75. gpio_set_value(host->clk_gpio, 0);
  76. return ret;
  77. }
  78. /**
  79. * cbus_receive_word - receives 16-bit word from the bus
  80. * @host: the host we're using
  81. */
  82. static int cbus_receive_word(struct cbus_host *host)
  83. {
  84. int ret = 0;
  85. int i;
  86. for (i = 16; i > 0; i--) {
  87. int bit = cbus_receive_bit(host);
  88. if (bit < 0)
  89. return bit;
  90. if (bit)
  91. ret |= 1 << (i - 1);
  92. }
  93. return ret;
  94. }
  95. /**
  96. * cbus_transfer - transfers data over the bus
  97. * @host: the host we're using
  98. * @rw: read/write flag
  99. * @dev: device address
  100. * @reg: register address
  101. * @data: if @rw == I2C_SBUS_WRITE data to send otherwise 0
  102. */
  103. static int cbus_transfer(struct cbus_host *host, char rw, unsigned dev,
  104. unsigned reg, unsigned data)
  105. {
  106. unsigned long flags;
  107. int ret;
  108. /* We don't want interrupts disturbing our transfer */
  109. spin_lock_irqsave(&host->lock, flags);
  110. /* Reset state and start of transfer, SEL stays down during transfer */
  111. gpio_set_value(host->sel_gpio, 0);
  112. /* Set the DAT pin to output */
  113. gpio_direction_output(host->dat_gpio, 1);
  114. /* Send the device address */
  115. cbus_send_data(host, dev, CBUS_ADDR_BITS);
  116. /* Send the rw flag */
  117. cbus_send_bit(host, rw == I2C_SMBUS_READ);
  118. /* Send the register address */
  119. cbus_send_data(host, reg, CBUS_REG_BITS);
  120. if (rw == I2C_SMBUS_WRITE) {
  121. cbus_send_data(host, data, 16);
  122. ret = 0;
  123. } else {
  124. ret = gpio_direction_input(host->dat_gpio);
  125. if (ret) {
  126. dev_dbg(host->dev, "failed setting direction\n");
  127. goto out;
  128. }
  129. gpio_set_value(host->clk_gpio, 1);
  130. ret = cbus_receive_word(host);
  131. if (ret < 0) {
  132. dev_dbg(host->dev, "failed receiving data\n");
  133. goto out;
  134. }
  135. }
  136. /* Indicate end of transfer, SEL goes up until next transfer */
  137. gpio_set_value(host->sel_gpio, 1);
  138. gpio_set_value(host->clk_gpio, 1);
  139. gpio_set_value(host->clk_gpio, 0);
  140. out:
  141. spin_unlock_irqrestore(&host->lock, flags);
  142. return ret;
  143. }
  144. static int cbus_i2c_smbus_xfer(struct i2c_adapter *adapter,
  145. u16 addr,
  146. unsigned short flags,
  147. char read_write,
  148. u8 command,
  149. int size,
  150. union i2c_smbus_data *data)
  151. {
  152. struct cbus_host *chost = i2c_get_adapdata(adapter);
  153. int ret;
  154. if (size != I2C_SMBUS_WORD_DATA)
  155. return -EINVAL;
  156. ret = cbus_transfer(chost, read_write == I2C_SMBUS_READ, addr,
  157. command, data->word);
  158. if (ret < 0)
  159. return ret;
  160. if (read_write == I2C_SMBUS_READ)
  161. data->word = ret;
  162. return 0;
  163. }
  164. static u32 cbus_i2c_func(struct i2c_adapter *adapter)
  165. {
  166. return I2C_FUNC_SMBUS_READ_WORD_DATA | I2C_FUNC_SMBUS_WRITE_WORD_DATA;
  167. }
  168. static const struct i2c_algorithm cbus_i2c_algo = {
  169. .smbus_xfer = cbus_i2c_smbus_xfer,
  170. .functionality = cbus_i2c_func,
  171. };
  172. static int cbus_i2c_remove(struct platform_device *pdev)
  173. {
  174. struct i2c_adapter *adapter = platform_get_drvdata(pdev);
  175. i2c_del_adapter(adapter);
  176. return 0;
  177. }
  178. static int cbus_i2c_probe(struct platform_device *pdev)
  179. {
  180. struct i2c_adapter *adapter;
  181. struct cbus_host *chost;
  182. int ret;
  183. adapter = devm_kzalloc(&pdev->dev, sizeof(struct i2c_adapter),
  184. GFP_KERNEL);
  185. if (!adapter)
  186. return -ENOMEM;
  187. chost = devm_kzalloc(&pdev->dev, sizeof(*chost), GFP_KERNEL);
  188. if (!chost)
  189. return -ENOMEM;
  190. if (pdev->dev.of_node) {
  191. struct device_node *dnode = pdev->dev.of_node;
  192. if (of_gpio_count(dnode) != 3)
  193. return -ENODEV;
  194. chost->clk_gpio = of_get_gpio(dnode, 0);
  195. chost->dat_gpio = of_get_gpio(dnode, 1);
  196. chost->sel_gpio = of_get_gpio(dnode, 2);
  197. } else if (dev_get_platdata(&pdev->dev)) {
  198. struct i2c_cbus_platform_data *pdata =
  199. dev_get_platdata(&pdev->dev);
  200. chost->clk_gpio = pdata->clk_gpio;
  201. chost->dat_gpio = pdata->dat_gpio;
  202. chost->sel_gpio = pdata->sel_gpio;
  203. } else {
  204. return -ENODEV;
  205. }
  206. adapter->owner = THIS_MODULE;
  207. adapter->class = I2C_CLASS_HWMON;
  208. adapter->dev.parent = &pdev->dev;
  209. adapter->dev.of_node = pdev->dev.of_node;
  210. adapter->nr = pdev->id;
  211. adapter->timeout = HZ;
  212. adapter->algo = &cbus_i2c_algo;
  213. strlcpy(adapter->name, "CBUS I2C adapter", sizeof(adapter->name));
  214. spin_lock_init(&chost->lock);
  215. chost->dev = &pdev->dev;
  216. ret = devm_gpio_request_one(&pdev->dev, chost->clk_gpio,
  217. GPIOF_OUT_INIT_LOW, "CBUS clk");
  218. if (ret)
  219. return ret;
  220. ret = devm_gpio_request_one(&pdev->dev, chost->dat_gpio, GPIOF_IN,
  221. "CBUS data");
  222. if (ret)
  223. return ret;
  224. ret = devm_gpio_request_one(&pdev->dev, chost->sel_gpio,
  225. GPIOF_OUT_INIT_HIGH, "CBUS sel");
  226. if (ret)
  227. return ret;
  228. i2c_set_adapdata(adapter, chost);
  229. platform_set_drvdata(pdev, adapter);
  230. return i2c_add_numbered_adapter(adapter);
  231. }
  232. #if defined(CONFIG_OF)
  233. static const struct of_device_id i2c_cbus_dt_ids[] = {
  234. { .compatible = "i2c-cbus-gpio", },
  235. { }
  236. };
  237. MODULE_DEVICE_TABLE(of, i2c_cbus_dt_ids);
  238. #endif
  239. static struct platform_driver cbus_i2c_driver = {
  240. .probe = cbus_i2c_probe,
  241. .remove = cbus_i2c_remove,
  242. .driver = {
  243. .name = "i2c-cbus-gpio",
  244. .of_match_table = of_match_ptr(i2c_cbus_dt_ids),
  245. },
  246. };
  247. module_platform_driver(cbus_i2c_driver);
  248. MODULE_ALIAS("platform:i2c-cbus-gpio");
  249. MODULE_DESCRIPTION("CBUS I2C driver");
  250. MODULE_AUTHOR("Juha Yrjölä");
  251. MODULE_AUTHOR("David Weinehall");
  252. MODULE_AUTHOR("Mikko Ylinen");
  253. MODULE_AUTHOR("Felipe Balbi");
  254. MODULE_AUTHOR("Aaro Koskinen <aaro.koskinen@iki.fi>");
  255. MODULE_LICENSE("GPL");