i2c.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * Generic i2c interface for ALSA
  3. *
  4. * (c) 1998 Gerd Knorr <kraxel@cs.tu-berlin.de>
  5. * Modified for the ALSA driver by Jaroslav Kysela <perex@suse.cz>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. #include <sound/driver.h>
  23. #include <linux/init.h>
  24. #include <linux/slab.h>
  25. #include <linux/string.h>
  26. #include <linux/errno.h>
  27. #include <sound/core.h>
  28. #include <sound/i2c.h>
  29. MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
  30. MODULE_DESCRIPTION("Generic i2c interface for ALSA");
  31. MODULE_LICENSE("GPL");
  32. static int snd_i2c_bit_sendbytes(snd_i2c_device_t *device, unsigned char *bytes, int count);
  33. static int snd_i2c_bit_readbytes(snd_i2c_device_t *device, unsigned char *bytes, int count);
  34. static int snd_i2c_bit_probeaddr(snd_i2c_bus_t *bus, unsigned short addr);
  35. static snd_i2c_ops_t snd_i2c_bit_ops = {
  36. .sendbytes = snd_i2c_bit_sendbytes,
  37. .readbytes = snd_i2c_bit_readbytes,
  38. .probeaddr = snd_i2c_bit_probeaddr,
  39. };
  40. static int snd_i2c_bus_free(snd_i2c_bus_t *bus)
  41. {
  42. snd_i2c_bus_t *slave;
  43. snd_i2c_device_t *device;
  44. snd_assert(bus != NULL, return -EINVAL);
  45. while (!list_empty(&bus->devices)) {
  46. device = snd_i2c_device(bus->devices.next);
  47. snd_i2c_device_free(device);
  48. }
  49. if (bus->master)
  50. list_del(&bus->buses);
  51. else {
  52. while (!list_empty(&bus->buses)) {
  53. slave = snd_i2c_slave_bus(bus->buses.next);
  54. snd_device_free(bus->card, slave);
  55. }
  56. }
  57. if (bus->private_free)
  58. bus->private_free(bus);
  59. kfree(bus);
  60. return 0;
  61. }
  62. static int snd_i2c_bus_dev_free(snd_device_t *device)
  63. {
  64. snd_i2c_bus_t *bus = device->device_data;
  65. return snd_i2c_bus_free(bus);
  66. }
  67. int snd_i2c_bus_create(snd_card_t *card, const char *name, snd_i2c_bus_t *master, snd_i2c_bus_t **ri2c)
  68. {
  69. snd_i2c_bus_t *bus;
  70. int err;
  71. static snd_device_ops_t ops = {
  72. .dev_free = snd_i2c_bus_dev_free,
  73. };
  74. *ri2c = NULL;
  75. bus = kcalloc(1, sizeof(*bus), GFP_KERNEL);
  76. if (bus == NULL)
  77. return -ENOMEM;
  78. init_MUTEX(&bus->lock_mutex);
  79. INIT_LIST_HEAD(&bus->devices);
  80. INIT_LIST_HEAD(&bus->buses);
  81. bus->card = card;
  82. bus->ops = &snd_i2c_bit_ops;
  83. if (master) {
  84. list_add_tail(&bus->buses, &master->buses);
  85. bus->master = master;
  86. }
  87. strlcpy(bus->name, name, sizeof(bus->name));
  88. if ((err = snd_device_new(card, SNDRV_DEV_BUS, bus, &ops)) < 0) {
  89. snd_i2c_bus_free(bus);
  90. return err;
  91. }
  92. *ri2c = bus;
  93. return 0;
  94. }
  95. int snd_i2c_device_create(snd_i2c_bus_t *bus, const char *name, unsigned char addr, snd_i2c_device_t **rdevice)
  96. {
  97. snd_i2c_device_t *device;
  98. *rdevice = NULL;
  99. snd_assert(bus != NULL, return -EINVAL);
  100. device = kcalloc(1, sizeof(*device), GFP_KERNEL);
  101. if (device == NULL)
  102. return -ENOMEM;
  103. device->addr = addr;
  104. strlcpy(device->name, name, sizeof(device->name));
  105. list_add_tail(&device->list, &bus->devices);
  106. device->bus = bus;
  107. *rdevice = device;
  108. return 0;
  109. }
  110. int snd_i2c_device_free(snd_i2c_device_t *device)
  111. {
  112. if (device->bus)
  113. list_del(&device->list);
  114. if (device->private_free)
  115. device->private_free(device);
  116. kfree(device);
  117. return 0;
  118. }
  119. int snd_i2c_sendbytes(snd_i2c_device_t *device, unsigned char *bytes, int count)
  120. {
  121. return device->bus->ops->sendbytes(device, bytes, count);
  122. }
  123. int snd_i2c_readbytes(snd_i2c_device_t *device, unsigned char *bytes, int count)
  124. {
  125. return device->bus->ops->readbytes(device, bytes, count);
  126. }
  127. int snd_i2c_probeaddr(snd_i2c_bus_t *bus, unsigned short addr)
  128. {
  129. return bus->ops->probeaddr(bus, addr);
  130. }
  131. /*
  132. * bit-operations
  133. */
  134. static inline void snd_i2c_bit_hw_start(snd_i2c_bus_t *bus)
  135. {
  136. if (bus->hw_ops.bit->start)
  137. bus->hw_ops.bit->start(bus);
  138. }
  139. static inline void snd_i2c_bit_hw_stop(snd_i2c_bus_t *bus)
  140. {
  141. if (bus->hw_ops.bit->stop)
  142. bus->hw_ops.bit->stop(bus);
  143. }
  144. static void snd_i2c_bit_direction(snd_i2c_bus_t *bus, int clock, int data)
  145. {
  146. if (bus->hw_ops.bit->direction)
  147. bus->hw_ops.bit->direction(bus, clock, data);
  148. }
  149. static void snd_i2c_bit_set(snd_i2c_bus_t *bus, int clock, int data)
  150. {
  151. bus->hw_ops.bit->setlines(bus, clock, data);
  152. }
  153. #if 0
  154. static int snd_i2c_bit_clock(snd_i2c_bus_t *bus)
  155. {
  156. if (bus->hw_ops.bit->getclock)
  157. return bus->hw_ops.bit->getclock(bus);
  158. return -ENXIO;
  159. }
  160. #endif
  161. static int snd_i2c_bit_data(snd_i2c_bus_t *bus, int ack)
  162. {
  163. return bus->hw_ops.bit->getdata(bus, ack);
  164. }
  165. static void snd_i2c_bit_start(snd_i2c_bus_t *bus)
  166. {
  167. snd_i2c_bit_hw_start(bus);
  168. snd_i2c_bit_direction(bus, 1, 1); /* SCL - wr, SDA - wr */
  169. snd_i2c_bit_set(bus, 1, 1);
  170. snd_i2c_bit_set(bus, 1, 0);
  171. snd_i2c_bit_set(bus, 0, 0);
  172. }
  173. static void snd_i2c_bit_stop(snd_i2c_bus_t *bus)
  174. {
  175. snd_i2c_bit_set(bus, 0, 0);
  176. snd_i2c_bit_set(bus, 1, 0);
  177. snd_i2c_bit_set(bus, 1, 1);
  178. snd_i2c_bit_hw_stop(bus);
  179. }
  180. static void snd_i2c_bit_send(snd_i2c_bus_t *bus, int data)
  181. {
  182. snd_i2c_bit_set(bus, 0, data);
  183. snd_i2c_bit_set(bus, 1, data);
  184. snd_i2c_bit_set(bus, 0, data);
  185. }
  186. static int snd_i2c_bit_ack(snd_i2c_bus_t *bus)
  187. {
  188. int ack;
  189. snd_i2c_bit_set(bus, 0, 1);
  190. snd_i2c_bit_set(bus, 1, 1);
  191. snd_i2c_bit_direction(bus, 1, 0); /* SCL - wr, SDA - rd */
  192. ack = snd_i2c_bit_data(bus, 1);
  193. snd_i2c_bit_direction(bus, 1, 1); /* SCL - wr, SDA - wr */
  194. snd_i2c_bit_set(bus, 0, 1);
  195. return ack ? -EIO : 0;
  196. }
  197. static int snd_i2c_bit_sendbyte(snd_i2c_bus_t *bus, unsigned char data)
  198. {
  199. int i, err;
  200. for (i = 7; i >= 0; i--)
  201. snd_i2c_bit_send(bus, !!(data & (1 << i)));
  202. if ((err = snd_i2c_bit_ack(bus)) < 0)
  203. return err;
  204. return 0;
  205. }
  206. static int snd_i2c_bit_readbyte(snd_i2c_bus_t *bus, int last)
  207. {
  208. int i;
  209. unsigned char data = 0;
  210. snd_i2c_bit_set(bus, 0, 1);
  211. snd_i2c_bit_direction(bus, 1, 0); /* SCL - wr, SDA - rd */
  212. for (i = 7; i >= 0; i--) {
  213. snd_i2c_bit_set(bus, 1, 1);
  214. if (snd_i2c_bit_data(bus, 0))
  215. data |= (1 << i);
  216. snd_i2c_bit_set(bus, 0, 1);
  217. }
  218. snd_i2c_bit_direction(bus, 1, 1); /* SCL - wr, SDA - wr */
  219. snd_i2c_bit_send(bus, !!last);
  220. return data;
  221. }
  222. static int snd_i2c_bit_sendbytes(snd_i2c_device_t *device, unsigned char *bytes, int count)
  223. {
  224. snd_i2c_bus_t *bus = device->bus;
  225. int err, res = 0;
  226. if (device->flags & SND_I2C_DEVICE_ADDRTEN)
  227. return -EIO; /* not yet implemented */
  228. snd_i2c_bit_start(bus);
  229. if ((err = snd_i2c_bit_sendbyte(bus, device->addr << 1)) < 0) {
  230. snd_i2c_bit_hw_stop(bus);
  231. return err;
  232. }
  233. while (count-- > 0) {
  234. if ((err = snd_i2c_bit_sendbyte(bus, *bytes++)) < 0) {
  235. snd_i2c_bit_hw_stop(bus);
  236. return err;
  237. }
  238. res++;
  239. }
  240. snd_i2c_bit_stop(bus);
  241. return res;
  242. }
  243. static int snd_i2c_bit_readbytes(snd_i2c_device_t *device, unsigned char *bytes, int count)
  244. {
  245. snd_i2c_bus_t *bus = device->bus;
  246. int err, res = 0;
  247. if (device->flags & SND_I2C_DEVICE_ADDRTEN)
  248. return -EIO; /* not yet implemented */
  249. snd_i2c_bit_start(bus);
  250. if ((err = snd_i2c_bit_sendbyte(bus, (device->addr << 1) | 1)) < 0) {
  251. snd_i2c_bit_hw_stop(bus);
  252. return err;
  253. }
  254. while (count-- > 0) {
  255. if ((err = snd_i2c_bit_readbyte(bus, count == 0)) < 0) {
  256. snd_i2c_bit_hw_stop(bus);
  257. return err;
  258. }
  259. *bytes++ = (unsigned char)err;
  260. res++;
  261. }
  262. snd_i2c_bit_stop(bus);
  263. return res;
  264. }
  265. static int snd_i2c_bit_probeaddr(snd_i2c_bus_t *bus, unsigned short addr)
  266. {
  267. int err;
  268. if (addr & 0x8000) /* 10-bit address */
  269. return -EIO; /* not yet implemented */
  270. if (addr & 0x7f80) /* invalid address */
  271. return -EINVAL;
  272. snd_i2c_bit_start(bus);
  273. err = snd_i2c_bit_sendbyte(bus, addr << 1);
  274. snd_i2c_bit_stop(bus);
  275. return err;
  276. }
  277. EXPORT_SYMBOL(snd_i2c_bus_create);
  278. EXPORT_SYMBOL(snd_i2c_device_create);
  279. EXPORT_SYMBOL(snd_i2c_device_free);
  280. EXPORT_SYMBOL(snd_i2c_sendbytes);
  281. EXPORT_SYMBOL(snd_i2c_readbytes);
  282. EXPORT_SYMBOL(snd_i2c_probeaddr);
  283. static int __init alsa_i2c_init(void)
  284. {
  285. return 0;
  286. }
  287. static void __exit alsa_i2c_exit(void)
  288. {
  289. }
  290. module_init(alsa_i2c_init)
  291. module_exit(alsa_i2c_exit)