i2c.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Generic i2c interface for ALSA
  4. *
  5. * (c) 1998 Gerd Knorr <[email protected]>
  6. * Modified for the ALSA driver by Jaroslav Kysela <[email protected]>
  7. */
  8. #include <linux/init.h>
  9. #include <linux/slab.h>
  10. #include <linux/module.h>
  11. #include <linux/string.h>
  12. #include <linux/errno.h>
  13. #include <sound/core.h>
  14. #include <sound/i2c.h>
  15. MODULE_AUTHOR("Jaroslav Kysela <[email protected]>");
  16. MODULE_DESCRIPTION("Generic i2c interface for ALSA");
  17. MODULE_LICENSE("GPL");
  18. static int snd_i2c_bit_sendbytes(struct snd_i2c_device *device,
  19. unsigned char *bytes, int count);
  20. static int snd_i2c_bit_readbytes(struct snd_i2c_device *device,
  21. unsigned char *bytes, int count);
  22. static int snd_i2c_bit_probeaddr(struct snd_i2c_bus *bus,
  23. unsigned short addr);
  24. static const struct snd_i2c_ops snd_i2c_bit_ops = {
  25. .sendbytes = snd_i2c_bit_sendbytes,
  26. .readbytes = snd_i2c_bit_readbytes,
  27. .probeaddr = snd_i2c_bit_probeaddr,
  28. };
  29. static int snd_i2c_bus_free(struct snd_i2c_bus *bus)
  30. {
  31. struct snd_i2c_bus *slave;
  32. struct snd_i2c_device *device;
  33. if (snd_BUG_ON(!bus))
  34. return -EINVAL;
  35. while (!list_empty(&bus->devices)) {
  36. device = snd_i2c_device(bus->devices.next);
  37. snd_i2c_device_free(device);
  38. }
  39. if (bus->master)
  40. list_del(&bus->buses);
  41. else {
  42. while (!list_empty(&bus->buses)) {
  43. slave = snd_i2c_slave_bus(bus->buses.next);
  44. snd_device_free(bus->card, slave);
  45. }
  46. }
  47. if (bus->private_free)
  48. bus->private_free(bus);
  49. kfree(bus);
  50. return 0;
  51. }
  52. static int snd_i2c_bus_dev_free(struct snd_device *device)
  53. {
  54. struct snd_i2c_bus *bus = device->device_data;
  55. return snd_i2c_bus_free(bus);
  56. }
  57. int snd_i2c_bus_create(struct snd_card *card, const char *name,
  58. struct snd_i2c_bus *master, struct snd_i2c_bus **ri2c)
  59. {
  60. struct snd_i2c_bus *bus;
  61. int err;
  62. static const struct snd_device_ops ops = {
  63. .dev_free = snd_i2c_bus_dev_free,
  64. };
  65. *ri2c = NULL;
  66. bus = kzalloc(sizeof(*bus), GFP_KERNEL);
  67. if (bus == NULL)
  68. return -ENOMEM;
  69. mutex_init(&bus->lock_mutex);
  70. INIT_LIST_HEAD(&bus->devices);
  71. INIT_LIST_HEAD(&bus->buses);
  72. bus->card = card;
  73. bus->ops = &snd_i2c_bit_ops;
  74. if (master) {
  75. list_add_tail(&bus->buses, &master->buses);
  76. bus->master = master;
  77. }
  78. strscpy(bus->name, name, sizeof(bus->name));
  79. err = snd_device_new(card, SNDRV_DEV_BUS, bus, &ops);
  80. if (err < 0) {
  81. snd_i2c_bus_free(bus);
  82. return err;
  83. }
  84. *ri2c = bus;
  85. return 0;
  86. }
  87. EXPORT_SYMBOL(snd_i2c_bus_create);
  88. int snd_i2c_device_create(struct snd_i2c_bus *bus, const char *name,
  89. unsigned char addr, struct snd_i2c_device **rdevice)
  90. {
  91. struct snd_i2c_device *device;
  92. *rdevice = NULL;
  93. if (snd_BUG_ON(!bus))
  94. return -EINVAL;
  95. device = kzalloc(sizeof(*device), GFP_KERNEL);
  96. if (device == NULL)
  97. return -ENOMEM;
  98. device->addr = addr;
  99. strscpy(device->name, name, sizeof(device->name));
  100. list_add_tail(&device->list, &bus->devices);
  101. device->bus = bus;
  102. *rdevice = device;
  103. return 0;
  104. }
  105. EXPORT_SYMBOL(snd_i2c_device_create);
  106. int snd_i2c_device_free(struct snd_i2c_device *device)
  107. {
  108. if (device->bus)
  109. list_del(&device->list);
  110. if (device->private_free)
  111. device->private_free(device);
  112. kfree(device);
  113. return 0;
  114. }
  115. EXPORT_SYMBOL(snd_i2c_device_free);
  116. int snd_i2c_sendbytes(struct snd_i2c_device *device, unsigned char *bytes, int count)
  117. {
  118. return device->bus->ops->sendbytes(device, bytes, count);
  119. }
  120. EXPORT_SYMBOL(snd_i2c_sendbytes);
  121. int snd_i2c_readbytes(struct snd_i2c_device *device, unsigned char *bytes, int count)
  122. {
  123. return device->bus->ops->readbytes(device, bytes, count);
  124. }
  125. EXPORT_SYMBOL(snd_i2c_readbytes);
  126. int snd_i2c_probeaddr(struct snd_i2c_bus *bus, unsigned short addr)
  127. {
  128. return bus->ops->probeaddr(bus, addr);
  129. }
  130. EXPORT_SYMBOL(snd_i2c_probeaddr);
  131. /*
  132. * bit-operations
  133. */
  134. static inline void snd_i2c_bit_hw_start(struct snd_i2c_bus *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(struct snd_i2c_bus *bus)
  140. {
  141. if (bus->hw_ops.bit->stop)
  142. bus->hw_ops.bit->stop(bus);
  143. }
  144. static void snd_i2c_bit_direction(struct snd_i2c_bus *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(struct snd_i2c_bus *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(struct snd_i2c_bus *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(struct snd_i2c_bus *bus, int ack)
  162. {
  163. return bus->hw_ops.bit->getdata(bus, ack);
  164. }
  165. static void snd_i2c_bit_start(struct snd_i2c_bus *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(struct snd_i2c_bus *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(struct snd_i2c_bus *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(struct snd_i2c_bus *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(struct snd_i2c_bus *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. err = snd_i2c_bit_ack(bus);
  203. if (err < 0)
  204. return err;
  205. return 0;
  206. }
  207. static int snd_i2c_bit_readbyte(struct snd_i2c_bus *bus, int last)
  208. {
  209. int i;
  210. unsigned char data = 0;
  211. snd_i2c_bit_set(bus, 0, 1);
  212. snd_i2c_bit_direction(bus, 1, 0); /* SCL - wr, SDA - rd */
  213. for (i = 7; i >= 0; i--) {
  214. snd_i2c_bit_set(bus, 1, 1);
  215. if (snd_i2c_bit_data(bus, 0))
  216. data |= (1 << i);
  217. snd_i2c_bit_set(bus, 0, 1);
  218. }
  219. snd_i2c_bit_direction(bus, 1, 1); /* SCL - wr, SDA - wr */
  220. snd_i2c_bit_send(bus, !!last);
  221. return data;
  222. }
  223. static int snd_i2c_bit_sendbytes(struct snd_i2c_device *device,
  224. unsigned char *bytes, int count)
  225. {
  226. struct snd_i2c_bus *bus = device->bus;
  227. int err, res = 0;
  228. if (device->flags & SND_I2C_DEVICE_ADDRTEN)
  229. return -EIO; /* not yet implemented */
  230. snd_i2c_bit_start(bus);
  231. err = snd_i2c_bit_sendbyte(bus, device->addr << 1);
  232. if (err < 0) {
  233. snd_i2c_bit_hw_stop(bus);
  234. return err;
  235. }
  236. while (count-- > 0) {
  237. err = snd_i2c_bit_sendbyte(bus, *bytes++);
  238. if (err < 0) {
  239. snd_i2c_bit_hw_stop(bus);
  240. return err;
  241. }
  242. res++;
  243. }
  244. snd_i2c_bit_stop(bus);
  245. return res;
  246. }
  247. static int snd_i2c_bit_readbytes(struct snd_i2c_device *device,
  248. unsigned char *bytes, int count)
  249. {
  250. struct snd_i2c_bus *bus = device->bus;
  251. int err, res = 0;
  252. if (device->flags & SND_I2C_DEVICE_ADDRTEN)
  253. return -EIO; /* not yet implemented */
  254. snd_i2c_bit_start(bus);
  255. err = snd_i2c_bit_sendbyte(bus, (device->addr << 1) | 1);
  256. if (err < 0) {
  257. snd_i2c_bit_hw_stop(bus);
  258. return err;
  259. }
  260. while (count-- > 0) {
  261. err = snd_i2c_bit_readbyte(bus, count == 0);
  262. if (err < 0) {
  263. snd_i2c_bit_hw_stop(bus);
  264. return err;
  265. }
  266. *bytes++ = (unsigned char)err;
  267. res++;
  268. }
  269. snd_i2c_bit_stop(bus);
  270. return res;
  271. }
  272. static int snd_i2c_bit_probeaddr(struct snd_i2c_bus *bus, unsigned short addr)
  273. {
  274. int err;
  275. if (addr & 0x8000) /* 10-bit address */
  276. return -EIO; /* not yet implemented */
  277. if (addr & 0x7f80) /* invalid address */
  278. return -EINVAL;
  279. snd_i2c_bit_start(bus);
  280. err = snd_i2c_bit_sendbyte(bus, addr << 1);
  281. snd_i2c_bit_stop(bus);
  282. return err;
  283. }