i2c-cbus-gpio.c 7.1 KB

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