i2c-sun6i-p2wi.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * P2WI (Push-Pull Two Wire Interface) bus driver.
  3. *
  4. * Author: Boris BREZILLON <boris.brezillon@free-electrons.com>
  5. *
  6. * This file is licensed under the terms of the GNU General Public License
  7. * version 2. This program is licensed "as is" without any warranty of any
  8. * kind, whether express or implied.
  9. *
  10. * The P2WI controller looks like an SMBus controller which only supports byte
  11. * data transfers. But, it differs from standard SMBus protocol on several
  12. * aspects:
  13. * - it supports only one slave device, and thus drop the address field
  14. * - it adds a parity bit every 8bits of data
  15. * - only one read access is required to read a byte (instead of a write
  16. * followed by a read access in standard SMBus protocol)
  17. * - there's no Ack bit after each byte transfer
  18. *
  19. * This means this bus cannot be used to interface with standard SMBus
  20. * devices (the only known device to support this interface is the AXP221
  21. * PMIC).
  22. *
  23. */
  24. #include <linux/clk.h>
  25. #include <linux/i2c.h>
  26. #include <linux/io.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/module.h>
  29. #include <linux/of.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/reset.h>
  32. /* P2WI registers */
  33. #define P2WI_CTRL 0x0
  34. #define P2WI_CCR 0x4
  35. #define P2WI_INTE 0x8
  36. #define P2WI_INTS 0xc
  37. #define P2WI_DADDR0 0x10
  38. #define P2WI_DADDR1 0x14
  39. #define P2WI_DLEN 0x18
  40. #define P2WI_DATA0 0x1c
  41. #define P2WI_DATA1 0x20
  42. #define P2WI_LCR 0x24
  43. #define P2WI_PMCR 0x28
  44. /* CTRL fields */
  45. #define P2WI_CTRL_START_TRANS BIT(7)
  46. #define P2WI_CTRL_ABORT_TRANS BIT(6)
  47. #define P2WI_CTRL_GLOBAL_INT_ENB BIT(1)
  48. #define P2WI_CTRL_SOFT_RST BIT(0)
  49. /* CLK CTRL fields */
  50. #define P2WI_CCR_SDA_OUT_DELAY(v) (((v) & 0x7) << 8)
  51. #define P2WI_CCR_MAX_CLK_DIV 0xff
  52. #define P2WI_CCR_CLK_DIV(v) ((v) & P2WI_CCR_MAX_CLK_DIV)
  53. /* STATUS fields */
  54. #define P2WI_INTS_TRANS_ERR_ID(v) (((v) >> 8) & 0xff)
  55. #define P2WI_INTS_LOAD_BSY BIT(2)
  56. #define P2WI_INTS_TRANS_ERR BIT(1)
  57. #define P2WI_INTS_TRANS_OVER BIT(0)
  58. /* DATA LENGTH fields*/
  59. #define P2WI_DLEN_READ BIT(4)
  60. #define P2WI_DLEN_DATA_LENGTH(v) ((v - 1) & 0x7)
  61. /* LINE CTRL fields*/
  62. #define P2WI_LCR_SCL_STATE BIT(5)
  63. #define P2WI_LCR_SDA_STATE BIT(4)
  64. #define P2WI_LCR_SCL_CTL BIT(3)
  65. #define P2WI_LCR_SCL_CTL_EN BIT(2)
  66. #define P2WI_LCR_SDA_CTL BIT(1)
  67. #define P2WI_LCR_SDA_CTL_EN BIT(0)
  68. /* PMU MODE CTRL fields */
  69. #define P2WI_PMCR_PMU_INIT_SEND BIT(31)
  70. #define P2WI_PMCR_PMU_INIT_DATA(v) (((v) & 0xff) << 16)
  71. #define P2WI_PMCR_PMU_MODE_REG(v) (((v) & 0xff) << 8)
  72. #define P2WI_PMCR_PMU_DEV_ADDR(v) ((v) & 0xff)
  73. #define P2WI_MAX_FREQ 6000000
  74. struct p2wi {
  75. struct i2c_adapter adapter;
  76. struct completion complete;
  77. unsigned int status;
  78. void __iomem *regs;
  79. struct clk *clk;
  80. struct reset_control *rstc;
  81. int slave_addr;
  82. };
  83. static irqreturn_t p2wi_interrupt(int irq, void *dev_id)
  84. {
  85. struct p2wi *p2wi = dev_id;
  86. unsigned long status;
  87. status = readl(p2wi->regs + P2WI_INTS);
  88. p2wi->status = status;
  89. /* Clear interrupts */
  90. status &= (P2WI_INTS_LOAD_BSY | P2WI_INTS_TRANS_ERR |
  91. P2WI_INTS_TRANS_OVER);
  92. writel(status, p2wi->regs + P2WI_INTS);
  93. complete(&p2wi->complete);
  94. return IRQ_HANDLED;
  95. }
  96. static u32 p2wi_functionality(struct i2c_adapter *adap)
  97. {
  98. return I2C_FUNC_SMBUS_BYTE_DATA;
  99. }
  100. static int p2wi_smbus_xfer(struct i2c_adapter *adap, u16 addr,
  101. unsigned short flags, char read_write,
  102. u8 command, int size, union i2c_smbus_data *data)
  103. {
  104. struct p2wi *p2wi = i2c_get_adapdata(adap);
  105. unsigned long dlen = P2WI_DLEN_DATA_LENGTH(1);
  106. if (p2wi->slave_addr >= 0 && addr != p2wi->slave_addr) {
  107. dev_err(&adap->dev, "invalid P2WI address\n");
  108. return -EINVAL;
  109. }
  110. if (!data)
  111. return -EINVAL;
  112. writel(command, p2wi->regs + P2WI_DADDR0);
  113. if (read_write == I2C_SMBUS_READ)
  114. dlen |= P2WI_DLEN_READ;
  115. else
  116. writel(data->byte, p2wi->regs + P2WI_DATA0);
  117. writel(dlen, p2wi->regs + P2WI_DLEN);
  118. if (readl(p2wi->regs + P2WI_CTRL) & P2WI_CTRL_START_TRANS) {
  119. dev_err(&adap->dev, "P2WI bus busy\n");
  120. return -EBUSY;
  121. }
  122. reinit_completion(&p2wi->complete);
  123. writel(P2WI_INTS_LOAD_BSY | P2WI_INTS_TRANS_ERR | P2WI_INTS_TRANS_OVER,
  124. p2wi->regs + P2WI_INTE);
  125. writel(P2WI_CTRL_START_TRANS | P2WI_CTRL_GLOBAL_INT_ENB,
  126. p2wi->regs + P2WI_CTRL);
  127. wait_for_completion(&p2wi->complete);
  128. if (p2wi->status & P2WI_INTS_LOAD_BSY) {
  129. dev_err(&adap->dev, "P2WI bus busy\n");
  130. return -EBUSY;
  131. }
  132. if (p2wi->status & P2WI_INTS_TRANS_ERR) {
  133. dev_err(&adap->dev, "P2WI bus xfer error\n");
  134. return -ENXIO;
  135. }
  136. if (read_write == I2C_SMBUS_READ)
  137. data->byte = readl(p2wi->regs + P2WI_DATA0);
  138. return 0;
  139. }
  140. static const struct i2c_algorithm p2wi_algo = {
  141. .smbus_xfer = p2wi_smbus_xfer,
  142. .functionality = p2wi_functionality,
  143. };
  144. static const struct of_device_id p2wi_of_match_table[] = {
  145. { .compatible = "allwinner,sun6i-a31-p2wi" },
  146. {}
  147. };
  148. MODULE_DEVICE_TABLE(of, p2wi_of_match_table);
  149. static int p2wi_probe(struct platform_device *pdev)
  150. {
  151. struct device *dev = &pdev->dev;
  152. struct device_node *np = dev->of_node;
  153. struct device_node *childnp;
  154. unsigned long parent_clk_freq;
  155. u32 clk_freq = 100000;
  156. struct resource *r;
  157. struct p2wi *p2wi;
  158. u32 slave_addr;
  159. int clk_div;
  160. int irq;
  161. int ret;
  162. of_property_read_u32(np, "clock-frequency", &clk_freq);
  163. if (clk_freq > P2WI_MAX_FREQ) {
  164. dev_err(dev,
  165. "required clock-frequency (%u Hz) is too high (max = 6MHz)",
  166. clk_freq);
  167. return -EINVAL;
  168. }
  169. if (of_get_child_count(np) > 1) {
  170. dev_err(dev, "P2WI only supports one slave device\n");
  171. return -EINVAL;
  172. }
  173. p2wi = devm_kzalloc(dev, sizeof(struct p2wi), GFP_KERNEL);
  174. if (!p2wi)
  175. return -ENOMEM;
  176. p2wi->slave_addr = -1;
  177. /*
  178. * Authorize a p2wi node without any children to be able to use an
  179. * i2c-dev from userpace.
  180. * In this case the slave_addr is set to -1 and won't be checked when
  181. * launching a P2WI transfer.
  182. */
  183. childnp = of_get_next_available_child(np, NULL);
  184. if (childnp) {
  185. ret = of_property_read_u32(childnp, "reg", &slave_addr);
  186. if (ret) {
  187. dev_err(dev, "invalid slave address on node %s\n",
  188. childnp->full_name);
  189. return -EINVAL;
  190. }
  191. p2wi->slave_addr = slave_addr;
  192. }
  193. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  194. p2wi->regs = devm_ioremap_resource(dev, r);
  195. if (IS_ERR(p2wi->regs))
  196. return PTR_ERR(p2wi->regs);
  197. strlcpy(p2wi->adapter.name, pdev->name, sizeof(p2wi->adapter.name));
  198. irq = platform_get_irq(pdev, 0);
  199. if (irq < 0) {
  200. dev_err(dev, "failed to retrieve irq: %d\n", irq);
  201. return irq;
  202. }
  203. p2wi->clk = devm_clk_get(dev, NULL);
  204. if (IS_ERR(p2wi->clk)) {
  205. ret = PTR_ERR(p2wi->clk);
  206. dev_err(dev, "failed to retrieve clk: %d\n", ret);
  207. return ret;
  208. }
  209. ret = clk_prepare_enable(p2wi->clk);
  210. if (ret) {
  211. dev_err(dev, "failed to enable clk: %d\n", ret);
  212. return ret;
  213. }
  214. parent_clk_freq = clk_get_rate(p2wi->clk);
  215. p2wi->rstc = devm_reset_control_get(dev, NULL);
  216. if (IS_ERR(p2wi->rstc)) {
  217. ret = PTR_ERR(p2wi->rstc);
  218. dev_err(dev, "failed to retrieve reset controller: %d\n", ret);
  219. goto err_clk_disable;
  220. }
  221. ret = reset_control_deassert(p2wi->rstc);
  222. if (ret) {
  223. dev_err(dev, "failed to deassert reset line: %d\n", ret);
  224. goto err_clk_disable;
  225. }
  226. init_completion(&p2wi->complete);
  227. p2wi->adapter.dev.parent = dev;
  228. p2wi->adapter.algo = &p2wi_algo;
  229. p2wi->adapter.owner = THIS_MODULE;
  230. p2wi->adapter.dev.of_node = pdev->dev.of_node;
  231. platform_set_drvdata(pdev, p2wi);
  232. i2c_set_adapdata(&p2wi->adapter, p2wi);
  233. ret = devm_request_irq(dev, irq, p2wi_interrupt, 0, pdev->name, p2wi);
  234. if (ret) {
  235. dev_err(dev, "can't register interrupt handler irq%d: %d\n",
  236. irq, ret);
  237. goto err_reset_assert;
  238. }
  239. writel(P2WI_CTRL_SOFT_RST, p2wi->regs + P2WI_CTRL);
  240. clk_div = parent_clk_freq / clk_freq;
  241. if (!clk_div) {
  242. dev_warn(dev,
  243. "clock-frequency is too high, setting it to %lu Hz\n",
  244. parent_clk_freq);
  245. clk_div = 1;
  246. } else if (clk_div > P2WI_CCR_MAX_CLK_DIV) {
  247. dev_warn(dev,
  248. "clock-frequency is too low, setting it to %lu Hz\n",
  249. parent_clk_freq / P2WI_CCR_MAX_CLK_DIV);
  250. clk_div = P2WI_CCR_MAX_CLK_DIV;
  251. }
  252. writel(P2WI_CCR_SDA_OUT_DELAY(1) | P2WI_CCR_CLK_DIV(clk_div),
  253. p2wi->regs + P2WI_CCR);
  254. ret = i2c_add_adapter(&p2wi->adapter);
  255. if (!ret)
  256. return 0;
  257. err_reset_assert:
  258. reset_control_assert(p2wi->rstc);
  259. err_clk_disable:
  260. clk_disable_unprepare(p2wi->clk);
  261. return ret;
  262. }
  263. static int p2wi_remove(struct platform_device *dev)
  264. {
  265. struct p2wi *p2wi = platform_get_drvdata(dev);
  266. reset_control_assert(p2wi->rstc);
  267. clk_disable_unprepare(p2wi->clk);
  268. i2c_del_adapter(&p2wi->adapter);
  269. return 0;
  270. }
  271. static struct platform_driver p2wi_driver = {
  272. .probe = p2wi_probe,
  273. .remove = p2wi_remove,
  274. .driver = {
  275. .name = "i2c-sunxi-p2wi",
  276. .of_match_table = p2wi_of_match_table,
  277. },
  278. };
  279. module_platform_driver(p2wi_driver);
  280. MODULE_AUTHOR("Boris BREZILLON <boris.brezillon@free-electrons.com>");
  281. MODULE_DESCRIPTION("Allwinner P2WI driver");
  282. MODULE_LICENSE("GPL v2");