em28xx-i2c.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. em2820-i2c.c - driver for Empia EM2820/2840 USB video capture devices
  3. Copyright (C) 2005 Markus Rechberger <mrechberger@gmail.com>
  4. Ludovico Cavedon <cavedon@sssup.it>
  5. Mauro Carvalho Chehab <mchehab@brturbo.com.br>
  6. Based on the em2800 driver from Sascha Sommer <saschasommer@freenet.de>
  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. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/kernel.h>
  21. #include <linux/usb.h>
  22. #include <linux/i2c.h>
  23. #include <linux/videodev.h>
  24. #include <media/tuner.h>
  25. #include <linux/video_decoder.h>
  26. /* To be moved to compat.h */
  27. #if !defined(I2C_HW_B_EM2820)
  28. #define I2C_HW_B_EM2820 I2C_HW_B_BT848
  29. #endif
  30. #include "em2820.h"
  31. /* ----------------------------------------------------------- */
  32. static unsigned int i2c_scan = 0;
  33. module_param(i2c_scan, int, 0444);
  34. MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
  35. static unsigned int i2c_debug = 0;
  36. module_param(i2c_debug, int, 0644);
  37. MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
  38. #define dprintk(fmt, args...) if (i2c_debug) do {\
  39. printk(KERN_DEBUG "%s: %s: " fmt "\n",\
  40. dev->name, __FUNCTION__ , ##args); } while (0)
  41. #define dprintk1(fmt, args...) if (i2c_debug) do{ \
  42. printk(KERN_DEBUG "%s: %s: " fmt, \
  43. dev->name, __FUNCTION__ , ##args); } while (0)
  44. #define dprintk2(fmt, args...) if (i2c_debug) do {\
  45. printk(fmt , ##args); } while (0)
  46. /*
  47. * i2c_send_bytes()
  48. * untested for more than 4 bytes
  49. */
  50. static int i2c_send_bytes(void *data, unsigned char addr, char *buf, short len,
  51. int stop)
  52. {
  53. int wrcount = 0;
  54. struct em2820 *dev = (struct em2820 *)data;
  55. wrcount = dev->em2820_write_regs_req(dev, stop ? 2 : 3, addr, buf, len);
  56. return wrcount;
  57. }
  58. /*
  59. * i2c_recv_byte()
  60. * read a byte from the i2c device
  61. */
  62. static int i2c_recv_bytes(struct em2820 *dev, unsigned char addr, char *buf,
  63. int len)
  64. {
  65. int ret;
  66. ret = dev->em2820_read_reg_req_len(dev, 2, addr, buf, len);
  67. if (ret < 0) {
  68. em2820_warn("reading i2c device failed (error=%i)\n", ret);
  69. return ret;
  70. }
  71. if (dev->em2820_read_reg(dev, 0x5) != 0)
  72. return -ENODEV;
  73. return ret;
  74. }
  75. /*
  76. * i2c_check_for_device()
  77. * check if there is a i2c_device at the supplied address
  78. */
  79. static int i2c_check_for_device(struct em2820 *dev, unsigned char addr)
  80. {
  81. char msg;
  82. int ret;
  83. msg = addr;
  84. ret = dev->em2820_read_reg_req(dev, 2, addr);
  85. if (ret < 0) {
  86. em2820_warn("reading from i2c device failed (error=%i)\n", ret);
  87. return ret;
  88. }
  89. if (dev->em2820_read_reg(dev, 0x5) != 0)
  90. return -ENODEV;
  91. return 0;
  92. }
  93. /*
  94. * em2820_i2c_xfer()
  95. * the main i2c transfer function
  96. */
  97. static int em2820_i2c_xfer(struct i2c_adapter *i2c_adap,
  98. struct i2c_msg msgs[], int num)
  99. {
  100. struct em2820 *dev = i2c_adap->algo_data;
  101. int addr, rc, i, byte;
  102. if (num <= 0)
  103. return 0;
  104. for (i = 0; i < num; i++) {
  105. addr = msgs[i].addr << 1;
  106. dprintk1("%s %s addr=%x len=%d:",
  107. (msgs[i].flags & I2C_M_RD) ? "read" : "write",
  108. i == num - 1 ? "stop" : "nonstop", addr, msgs[i].len);
  109. if (!msgs[i].len) { /* no len: check only for device presence */
  110. rc = i2c_check_for_device(dev, addr);
  111. if (rc < 0) {
  112. dprintk2(" no device\n");
  113. return rc;
  114. }
  115. }
  116. if (msgs[i].flags & I2C_M_RD) {
  117. /* read bytes */
  118. rc = i2c_recv_bytes(dev, addr, msgs[i].buf,
  119. msgs[i].len);
  120. if (i2c_debug) {
  121. for (byte = 0; byte < msgs[i].len; byte++) {
  122. printk(" %02x", msgs[i].buf[byte]);
  123. }
  124. }
  125. } else {
  126. /* write bytes */
  127. if (i2c_debug) {
  128. for (byte = 0; byte < msgs[i].len; byte++)
  129. printk(" %02x", msgs[i].buf[byte]);
  130. }
  131. rc = i2c_send_bytes(dev, addr, msgs[i].buf, msgs[i].len,
  132. i == num - 1);
  133. if (rc < 0)
  134. goto err;
  135. }
  136. if (i2c_debug)
  137. printk("\n");
  138. }
  139. return num;
  140. err:
  141. dprintk2(" ERROR: %i\n", rc);
  142. return rc;
  143. }
  144. static int em2820_i2c_eeprom(struct em2820 *dev, unsigned char *eedata, int len)
  145. {
  146. unsigned char buf, *p = eedata;
  147. struct em2820_eeprom *em_eeprom = (void *)eedata;
  148. int i, err, size = len, block;
  149. dev->i2c_client.addr = 0xa0 >> 1;
  150. buf = 0;
  151. if (1 != (err = i2c_master_send(&dev->i2c_client, &buf, 1))) {
  152. printk(KERN_INFO "%s: Huh, no eeprom present (err=%d)?\n",
  153. dev->name, err);
  154. return -1;
  155. }
  156. while (size > 0) {
  157. if (size > 16)
  158. block = 16;
  159. else
  160. block = size;
  161. if (block !=
  162. (err = i2c_master_recv(&dev->i2c_client, p, block))) {
  163. printk(KERN_WARNING
  164. "%s: i2c eeprom read error (err=%d)\n",
  165. dev->name, err);
  166. return -1;
  167. }
  168. size -= block;
  169. p += block;
  170. }
  171. for (i = 0; i < len; i++) {
  172. if (0 == (i % 16))
  173. printk(KERN_INFO "%s: i2c eeprom %02x:", dev->name, i);
  174. printk(" %02x", eedata[i]);
  175. if (15 == (i % 16))
  176. printk("\n");
  177. }
  178. printk(KERN_INFO "EEPROM ID= 0x%08x\n", em_eeprom->id);
  179. printk(KERN_INFO "Vendor/Product ID= %04x:%04x\n", em_eeprom->vendor_ID,
  180. em_eeprom->product_ID);
  181. switch (em_eeprom->chip_conf >> 4 & 0x3) {
  182. case 0:
  183. printk(KERN_INFO "No audio on board.\n");
  184. break;
  185. case 1:
  186. printk(KERN_INFO "AC97 audio (5 sample rates)\n");
  187. break;
  188. case 2:
  189. printk(KERN_INFO "I2S audio, sample rate=32k\n");
  190. break;
  191. case 3:
  192. printk(KERN_INFO "I2S audio, 3 sample rates\n");
  193. break;
  194. }
  195. if (em_eeprom->chip_conf & 1 << 3)
  196. printk(KERN_INFO "USB Remote wakeup capable\n");
  197. if (em_eeprom->chip_conf & 1 << 2)
  198. printk(KERN_INFO "USB Self power capable\n");
  199. switch (em_eeprom->chip_conf & 0x3) {
  200. case 0:
  201. printk(KERN_INFO "500mA max power\n");
  202. break;
  203. case 1:
  204. printk(KERN_INFO "400mA max power\n");
  205. break;
  206. case 2:
  207. printk(KERN_INFO "300mA max power\n");
  208. break;
  209. case 3:
  210. printk(KERN_INFO "200mA max power\n");
  211. break;
  212. }
  213. return 0;
  214. }
  215. /* ----------------------------------------------------------- */
  216. /*
  217. * algo_control()
  218. */
  219. static int algo_control(struct i2c_adapter *adapter,
  220. unsigned int cmd, unsigned long arg)
  221. {
  222. return 0;
  223. }
  224. /*
  225. * functionality()
  226. */
  227. static u32 functionality(struct i2c_adapter *adap)
  228. {
  229. return I2C_FUNC_SMBUS_EMUL;
  230. }
  231. #ifndef I2C_PEC
  232. static void inc_use(struct i2c_adapter *adap)
  233. {
  234. MOD_INC_USE_COUNT;
  235. }
  236. static void dec_use(struct i2c_adapter *adap)
  237. {
  238. MOD_DEC_USE_COUNT;
  239. }
  240. #endif
  241. static int em2820_set_tuner(int check_eeprom, struct i2c_client *client)
  242. {
  243. struct em2820 *dev = client->adapter->algo_data;
  244. struct tuner_setup tun_setup;
  245. if (dev->has_tuner) {
  246. tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
  247. tun_setup.type = dev->tuner_type;
  248. tun_setup.addr = dev->tuner_addr;
  249. em2820_i2c_call_clients(dev, TUNER_SET_TYPE_ADDR, &tun_setup);
  250. }
  251. return (0);
  252. }
  253. /*
  254. * attach_inform()
  255. * gets called when a device attaches to the i2c bus
  256. * does some basic configuration
  257. */
  258. static int attach_inform(struct i2c_client *client)
  259. {
  260. struct em2820 *dev = client->adapter->algo_data;
  261. dprintk("address %x", client->addr << 1);
  262. switch (client->addr << 1) {
  263. case 0x68:
  264. em2820_i2c_call_clients(dev, TDA9887_SET_CONFIG, &dev->tda9887_conf);
  265. break;
  266. case 0x4a:
  267. dprintk1("attach_inform: saa7113 detected.\n");
  268. break;
  269. case 0xa0:
  270. dprintk1("attach_inform: eeprom detected.\n");
  271. break;
  272. case 0x80:
  273. case 0x88:
  274. dprintk1("attach_inform: msp34xx detected.\n");
  275. break;
  276. case 0xb8:
  277. case 0xba:
  278. dprintk1("attach_inform: tvp5150 detected.\n");
  279. break;
  280. default:
  281. dev->tuner_addr = client->addr;
  282. em2820_set_tuner(-1, client);
  283. }
  284. return 0;
  285. }
  286. static struct i2c_algorithm em2820_algo = {
  287. .master_xfer = em2820_i2c_xfer,
  288. .algo_control = algo_control,
  289. .functionality = functionality,
  290. };
  291. static struct i2c_adapter em2820_adap_template = {
  292. #ifdef I2C_PEC
  293. .owner = THIS_MODULE,
  294. #else
  295. .inc_use = inc_use,
  296. .dec_use = dec_use,
  297. #endif
  298. #ifdef I2C_CLASS_TV_ANALOG
  299. .class = I2C_CLASS_TV_ANALOG,
  300. #endif
  301. .name = "em2820",
  302. .id = I2C_HW_B_EM2820,
  303. .algo = &em2820_algo,
  304. .client_register = attach_inform,
  305. };
  306. static struct i2c_client em2820_client_template = {
  307. .name = "em2820 internal",
  308. .flags = I2C_CLIENT_ALLOW_USE,
  309. };
  310. /* ----------------------------------------------------------- */
  311. /*
  312. * i2c_devs
  313. * incomplete list of known devices
  314. */
  315. static char *i2c_devs[128] = {
  316. [0x4a >> 1] = "saa7113h",
  317. [0x60 >> 1] = "remote IR sensor",
  318. [0x86 >> 1] = "tda9887",
  319. [0x80 >> 1] = "msp34xx",
  320. [0x88 >> 1] = "msp34xx",
  321. [0xa0 >> 1] = "eeprom",
  322. [0xb8 >> 1] = "tvp5150a",
  323. [0xba >> 1] = "tvp5150a",
  324. [0xc0 >> 1] = "tuner (analog)",
  325. [0xc2 >> 1] = "tuner (analog)",
  326. [0xc4 >> 1] = "tuner (analog)",
  327. [0xc6 >> 1] = "tuner (analog)",
  328. };
  329. /*
  330. * do_i2c_scan()
  331. * check i2c address range for devices
  332. */
  333. static void do_i2c_scan(char *name, struct i2c_client *c)
  334. {
  335. unsigned char buf;
  336. int i, rc;
  337. for (i = 0; i < 128; i++) {
  338. c->addr = i;
  339. rc = i2c_master_recv(c, &buf, 0);
  340. if (rc < 0)
  341. continue;
  342. printk(KERN_INFO "%s: found device @ 0x%x [%s]", name,
  343. i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
  344. }
  345. }
  346. /*
  347. * em2820_i2c_call_clients()
  348. * send commands to all attached i2c devices
  349. */
  350. void em2820_i2c_call_clients(struct em2820 *dev, unsigned int cmd, void *arg)
  351. {
  352. BUG_ON(NULL == dev->i2c_adap.algo_data);
  353. i2c_clients_command(&dev->i2c_adap, cmd, arg);
  354. }
  355. /*
  356. * em2820_i2c_register()
  357. * register i2c bus
  358. */
  359. int em2820_i2c_register(struct em2820 *dev)
  360. {
  361. BUG_ON(!dev->em2820_write_regs || !dev->em2820_read_reg);
  362. BUG_ON(!dev->em2820_write_regs_req || !dev->em2820_read_reg_req);
  363. dev->i2c_adap = em2820_adap_template;
  364. dev->i2c_adap.dev.parent = &dev->udev->dev;
  365. strcpy(dev->i2c_adap.name, dev->name);
  366. dev->i2c_adap.algo_data = dev;
  367. i2c_add_adapter(&dev->i2c_adap);
  368. dev->i2c_client = em2820_client_template;
  369. dev->i2c_client.adapter = &dev->i2c_adap;
  370. em2820_i2c_eeprom(dev, dev->eedata, sizeof(dev->eedata));
  371. if (i2c_scan)
  372. do_i2c_scan(dev->name, &dev->i2c_client);
  373. return 0;
  374. }
  375. /*
  376. * em2820_i2c_unregister()
  377. * unregister i2c_bus
  378. */
  379. int em2820_i2c_unregister(struct em2820 *dev)
  380. {
  381. i2c_del_adapter(&dev->i2c_adap);
  382. return 0;
  383. }