i2c.c 8.3 KB

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