em28xx-i2c.c 10 KB

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