l3.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * L3 code
  3. *
  4. * Copyright (C) 2008, Christian Pellegrin <chripell@evolware.org>
  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 version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. *
  11. * based on:
  12. *
  13. * L3 bus algorithm module.
  14. *
  15. * Copyright (C) 2001 Russell King, All Rights Reserved.
  16. *
  17. *
  18. */
  19. #include <linux/module.h>
  20. #include <linux/kernel.h>
  21. #include <linux/delay.h>
  22. #include <linux/device.h>
  23. #include <linux/gpio.h>
  24. #include <sound/l3.h>
  25. /*
  26. * Send one byte of data to the chip. Data is latched into the chip on
  27. * the rising edge of the clock.
  28. */
  29. static void sendbyte(struct l3_pins *adap, unsigned int byte)
  30. {
  31. int i;
  32. for (i = 0; i < 8; i++) {
  33. adap->setclk(adap, 0);
  34. udelay(adap->data_hold);
  35. adap->setdat(adap, byte & 1);
  36. udelay(adap->data_setup);
  37. adap->setclk(adap, 1);
  38. udelay(adap->clock_high);
  39. byte >>= 1;
  40. }
  41. }
  42. /*
  43. * Send a set of bytes to the chip. We need to pulse the MODE line
  44. * between each byte, but never at the start nor at the end of the
  45. * transfer.
  46. */
  47. static void sendbytes(struct l3_pins *adap, const u8 *buf,
  48. int len)
  49. {
  50. int i;
  51. for (i = 0; i < len; i++) {
  52. if (i) {
  53. udelay(adap->mode_hold);
  54. adap->setmode(adap, 0);
  55. udelay(adap->mode);
  56. }
  57. adap->setmode(adap, 1);
  58. udelay(adap->mode_setup);
  59. sendbyte(adap, buf[i]);
  60. }
  61. }
  62. int l3_write(struct l3_pins *adap, u8 addr, u8 *data, int len)
  63. {
  64. adap->setclk(adap, 1);
  65. adap->setdat(adap, 1);
  66. adap->setmode(adap, 1);
  67. udelay(adap->mode);
  68. adap->setmode(adap, 0);
  69. udelay(adap->mode_setup);
  70. sendbyte(adap, addr);
  71. udelay(adap->mode_hold);
  72. sendbytes(adap, data, len);
  73. adap->setclk(adap, 1);
  74. adap->setdat(adap, 1);
  75. adap->setmode(adap, 0);
  76. return len;
  77. }
  78. EXPORT_SYMBOL_GPL(l3_write);
  79. static void l3_set_clk(struct l3_pins *adap, int val)
  80. {
  81. gpio_set_value(adap->gpio_clk, val);
  82. }
  83. static void l3_set_data(struct l3_pins *adap, int val)
  84. {
  85. gpio_set_value(adap->gpio_data, val);
  86. }
  87. static void l3_set_mode(struct l3_pins *adap, int val)
  88. {
  89. gpio_set_value(adap->gpio_mode, val);
  90. }
  91. int l3_set_gpio_ops(struct device *dev, struct l3_pins *adap)
  92. {
  93. int ret;
  94. if (!adap->use_gpios)
  95. return -EINVAL;
  96. ret = devm_gpio_request_one(dev, adap->gpio_data,
  97. GPIOF_OUT_INIT_LOW, "l3_data");
  98. if (ret < 0)
  99. return ret;
  100. adap->setdat = l3_set_data;
  101. ret = devm_gpio_request_one(dev, adap->gpio_clk,
  102. GPIOF_OUT_INIT_LOW, "l3_clk");
  103. if (ret < 0)
  104. return ret;
  105. adap->setclk = l3_set_clk;
  106. ret = devm_gpio_request_one(dev, adap->gpio_mode,
  107. GPIOF_OUT_INIT_LOW, "l3_mode");
  108. if (ret < 0)
  109. return ret;
  110. adap->setmode = l3_set_mode;
  111. return 0;
  112. }
  113. EXPORT_SYMBOL_GPL(l3_set_gpio_ops);
  114. MODULE_DESCRIPTION("L3 bit-banging driver");
  115. MODULE_AUTHOR("Christian Pellegrin <chripell@evolware.org>");
  116. MODULE_LICENSE("GPL");