i2c.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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@perex.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 <linux/init.h>
  23. #include <linux/slab.h>
  24. #include <linux/string.h>
  25. #include <linux/errno.h>
  26. #include <sound/core.h>
  27. #include <sound/i2c.h>
  28. MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
  29. MODULE_DESCRIPTION("Generic i2c interface for ALSA");
  30. MODULE_LICENSE("GPL");
  31. static int snd_i2c_bit_sendbytes(struct snd_i2c_device *device,
  32. unsigned char *bytes, int count);
  33. static int snd_i2c_bit_readbytes(struct snd_i2c_device *device,
  34. unsigned char *bytes, int count);
  35. static int snd_i2c_bit_probeaddr(struct snd_i2c_bus *bus,
  36. unsigned short addr);
  37. static struct snd_i2c_ops snd_i2c_bit_ops = {
  38. .sendbytes = snd_i2c_bit_sendbytes,
  39. .readbytes = snd_i2c_bit_readbytes,
  40. .probeaddr = snd_i2c_bit_probeaddr,
  41. };
  42. static int snd_i2c_bus_free(struct snd_i2c_bus *bus)
  43. {
  44. struct snd_i2c_bus *slave;
  45. struct snd_i2c_device *device;
  46. snd_assert(bus != NULL, return -EINVAL);
  47. while (!list_empty(&bus->devices)) {
  48. device = snd_i2c_device(bus->devices.next);
  49. snd_i2c_device_free(device);
  50. }
  51. if (bus->master)
  52. list_del(&bus->buses);
  53. else {
  54. while (!list_empty(&bus->buses)) {
  55. slave = snd_i2c_slave_bus(bus->buses.next);
  56. snd_device_free(bus->card, slave);
  57. }
  58. }
  59. if (bus->private_free)
  60. bus->private_free(bus);
  61. kfree(bus);
  62. return 0;
  63. }
  64. static int snd_i2c_bus_dev_free(struct snd_device *device)
  65. {
  66. struct snd_i2c_bus *bus = device->device_data;
  67. return snd_i2c_bus_free(bus);
  68. }
  69. int snd_i2c_bus_create(struct snd_card *card, const char *name,
  70. struct snd_i2c_bus *master, struct snd_i2c_bus **ri2c)
  71. {
  72. struct snd_i2c_bus *bus;
  73. int err;
  74. static struct snd_device_ops ops = {
  75. .dev_free = snd_i2c_bus_dev_free,
  76. };
  77. *ri2c = NULL;
  78. bus = kzalloc(sizeof(*bus), GFP_KERNEL);
  79. if (bus == NULL)
  80. return -ENOMEM;
  81. mutex_init(&bus->lock_mutex);
  82. INIT_LIST_HEAD(&bus->devices);
  83. INIT_LIST_HEAD(&bus->buses);
  84. bus->card = card;
  85. bus->ops = &snd_i2c_bit_ops;
  86. if (master) {
  87. list_add_tail(&bus->buses, &master->buses);
  88. bus->master = master;
  89. }
  90. strlcpy(bus->name, name, sizeof(bus->name));
  91. if ((err = snd_device_new(card, SNDRV_DEV_BUS, bus, &ops)) < 0) {
  92. snd_i2c_bus_free(bus);
  93. return err;
  94. }
  95. *ri2c = bus;
  96. return 0;
  97. }
  98. EXPORT_SYMBOL(snd_i2c_bus_create);
  99. int snd_i2c_device_create(struct snd_i2c_bus *bus, const char *name,
  100. unsigned char addr, struct snd_i2c_device **rdevice)
  101. {
  102. struct snd_i2c_device *device;
  103. *rdevice = NULL;
  104. snd_assert(bus != NULL, return -EINVAL);
  105. device = kzalloc(sizeof(*device), GFP_KERNEL);
  106. if (device == NULL)
  107. return -ENOMEM;
  108. device->addr = addr;
  109. strlcpy(device->name, name, sizeof(device->name));
  110. list_add_tail(&device->list, &bus->devices);
  111. device->bus = bus;
  112. *rdevice = device;
  113. return 0;
  114. }
  115. EXPORT_SYMBOL(snd_i2c_device_create);
  116. int snd_i2c_device_free(struct snd_i2c_device *device)
  117. {
  118. if (device->bus)
  119. list_del(&device->list);
  120. if (device->private_free)
  121. device->private_free(device);
  122. kfree(device);
  123. return 0;
  124. }
  125. EXPORT_SYMBOL(snd_i2c_device_free);
  126. int snd_i2c_sendbytes(struct snd_i2c_device *device, unsigned char *bytes, int count)
  127. {
  128. return device->bus->ops->sendbytes(device, bytes, count);
  129. }
  130. EXPORT_SYMBOL(snd_i2c_sendbytes);
  131. int snd_i2c_readbytes(struct snd_i2c_device *device, unsigned char *bytes, int count)
  132. {
  133. return device->bus->ops->readbytes(device, bytes, count);
  134. }
  135. EXPORT_SYMBOL(snd_i2c_readbytes);
  136. int snd_i2c_probeaddr(struct snd_i2c_bus *bus, unsigned short addr)
  137. {
  138. return bus->ops->probeaddr(bus, addr);
  139. }
  140. EXPORT_SYMBOL(snd_i2c_probeaddr);
  141. /*
  142. * bit-operations
  143. */
  144. static inline void snd_i2c_bit_hw_start(struct snd_i2c_bus *bus)
  145. {
  146. if (bus->hw_ops.bit->start)
  147. bus->hw_ops.bit->start(bus);
  148. }
  149. static inline void snd_i2c_bit_hw_stop(struct snd_i2c_bus *bus)
  150. {
  151. if (bus->hw_ops.bit->stop)
  152. bus->hw_ops.bit->stop(bus);
  153. }
  154. static void snd_i2c_bit_direction(struct snd_i2c_bus *bus, int clock, int data)
  155. {
  156. if (bus->hw_ops.bit->direction)
  157. bus->hw_ops.bit->direction(bus, clock, data);
  158. }
  159. static void snd_i2c_bit_set(struct snd_i2c_bus *bus, int clock, int data)
  160. {
  161. bus->hw_ops.bit->setlines(bus, clock, data);
  162. }
  163. #if 0
  164. static int snd_i2c_bit_clock(struct snd_i2c_bus *bus)
  165. {
  166. if (bus->hw_ops.bit->getclock)
  167. return bus->hw_ops.bit->getclock(bus);
  168. return -ENXIO;
  169. }
  170. #endif
  171. static int snd_i2c_bit_data(struct snd_i2c_bus *bus, int ack)
  172. {
  173. return bus->hw_ops.bit->getdata(bus, ack);
  174. }
  175. static void snd_i2c_bit_start(struct snd_i2c_bus *bus)
  176. {
  177. snd_i2c_bit_hw_start(bus);
  178. snd_i2c_bit_direction(bus, 1, 1); /* SCL - wr, SDA - wr */
  179. snd_i2c_bit_set(bus, 1, 1);
  180. snd_i2c_bit_set(bus, 1, 0);
  181. snd_i2c_bit_set(bus, 0, 0);
  182. }
  183. static void snd_i2c_bit_stop(struct snd_i2c_bus *bus)
  184. {
  185. snd_i2c_bit_set(bus, 0, 0);
  186. snd_i2c_bit_set(bus, 1, 0);
  187. snd_i2c_bit_set(bus, 1, 1);
  188. snd_i2c_bit_hw_stop(bus);
  189. }
  190. static void snd_i2c_bit_send(struct snd_i2c_bus *bus, int data)
  191. {
  192. snd_i2c_bit_set(bus, 0, data);
  193. snd_i2c_bit_set(bus, 1, data);
  194. snd_i2c_bit_set(bus, 0, data);
  195. }
  196. static int snd_i2c_bit_ack(struct snd_i2c_bus *bus)
  197. {
  198. int ack;
  199. snd_i2c_bit_set(bus, 0, 1);
  200. snd_i2c_bit_set(bus, 1, 1);
  201. snd_i2c_bit_direction(bus, 1, 0); /* SCL - wr, SDA - rd */
  202. ack = snd_i2c_bit_data(bus, 1);
  203. snd_i2c_bit_direction(bus, 1, 1); /* SCL - wr, SDA - wr */
  204. snd_i2c_bit_set(bus, 0, 1);
  205. return ack ? -EIO : 0;
  206. }
  207. static int snd_i2c_bit_sendbyte(struct snd_i2c_bus *bus, unsigned char data)
  208. {
  209. int i, err;
  210. for (i = 7; i >= 0; i--)
  211. snd_i2c_bit_send(bus, !!(data & (1 << i)));
  212. if ((err = snd_i2c_bit_ack(bus)) < 0)
  213. return err;
  214. return 0;
  215. }
  216. static int snd_i2c_bit_readbyte(struct snd_i2c_bus *bus, int last)
  217. {
  218. int i;
  219. unsigned char data = 0;
  220. snd_i2c_bit_set(bus, 0, 1);
  221. snd_i2c_bit_direction(bus, 1, 0); /* SCL - wr, SDA - rd */
  222. for (i = 7; i >= 0; i--) {
  223. snd_i2c_bit_set(bus, 1, 1);
  224. if (snd_i2c_bit_data(bus, 0))
  225. data |= (1 << i);
  226. snd_i2c_bit_set(bus, 0, 1);
  227. }
  228. snd_i2c_bit_direction(bus, 1, 1); /* SCL - wr, SDA - wr */
  229. snd_i2c_bit_send(bus, !!last);
  230. return data;
  231. }
  232. static int snd_i2c_bit_sendbytes(struct snd_i2c_device *device,
  233. unsigned char *bytes, int count)
  234. {
  235. struct snd_i2c_bus *bus = device->bus;
  236. int err, res = 0;
  237. if (device->flags & SND_I2C_DEVICE_ADDRTEN)
  238. return -EIO; /* not yet implemented */
  239. snd_i2c_bit_start(bus);
  240. if ((err = snd_i2c_bit_sendbyte(bus, device->addr << 1)) < 0) {
  241. snd_i2c_bit_hw_stop(bus);
  242. return err;
  243. }
  244. while (count-- > 0) {
  245. if ((err = snd_i2c_bit_sendbyte(bus, *bytes++)) < 0) {
  246. snd_i2c_bit_hw_stop(bus);
  247. return err;
  248. }
  249. res++;
  250. }
  251. snd_i2c_bit_stop(bus);
  252. return res;
  253. }
  254. static int snd_i2c_bit_readbytes(struct snd_i2c_device *device,
  255. unsigned char *bytes, int count)
  256. {
  257. struct snd_i2c_bus *bus = device->bus;
  258. int err, res = 0;
  259. if (device->flags & SND_I2C_DEVICE_ADDRTEN)
  260. return -EIO; /* not yet implemented */
  261. snd_i2c_bit_start(bus);
  262. if ((err = snd_i2c_bit_sendbyte(bus, (device->addr << 1) | 1)) < 0) {
  263. snd_i2c_bit_hw_stop(bus);
  264. return err;
  265. }
  266. while (count-- > 0) {
  267. if ((err = snd_i2c_bit_readbyte(bus, count == 0)) < 0) {
  268. snd_i2c_bit_hw_stop(bus);
  269. return err;
  270. }
  271. *bytes++ = (unsigned char)err;
  272. res++;
  273. }
  274. snd_i2c_bit_stop(bus);
  275. return res;
  276. }
  277. static int snd_i2c_bit_probeaddr(struct snd_i2c_bus *bus, unsigned short addr)
  278. {
  279. int err;
  280. if (addr & 0x8000) /* 10-bit address */
  281. return -EIO; /* not yet implemented */
  282. if (addr & 0x7f80) /* invalid address */
  283. return -EINVAL;
  284. snd_i2c_bit_start(bus);
  285. err = snd_i2c_bit_sendbyte(bus, addr << 1);
  286. snd_i2c_bit_stop(bus);
  287. return err;
  288. }
  289. static int __init alsa_i2c_init(void)
  290. {
  291. return 0;
  292. }
  293. static void __exit alsa_i2c_exit(void)
  294. {
  295. }
  296. module_init(alsa_i2c_init)
  297. module_exit(alsa_i2c_exit)