cx18-i2c.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*
  2. * cx18 I2C functions
  3. *
  4. * Derived from ivtv-i2c.c
  5. *
  6. * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  21. * 02111-1307 USA
  22. */
  23. #include "cx18-driver.h"
  24. #include "cx18-io.h"
  25. #include "cx18-cards.h"
  26. #include "cx18-gpio.h"
  27. #include "cx18-av-core.h"
  28. #include "cx18-i2c.h"
  29. #include "cx18-irq.h"
  30. #define CX18_REG_I2C_1_WR 0xf15000
  31. #define CX18_REG_I2C_1_RD 0xf15008
  32. #define CX18_REG_I2C_2_WR 0xf25100
  33. #define CX18_REG_I2C_2_RD 0xf25108
  34. #define SETSCL_BIT 0x0001
  35. #define SETSDL_BIT 0x0002
  36. #define GETSCL_BIT 0x0004
  37. #define GETSDL_BIT 0x0008
  38. #define CX18_CS5345_I2C_ADDR 0x4c
  39. /* This array should match the CX18_HW_ defines */
  40. static const u8 hw_driverids[] = {
  41. I2C_DRIVERID_TUNER,
  42. I2C_DRIVERID_TVEEPROM,
  43. I2C_DRIVERID_CS5345,
  44. 0, /* CX18_HW_GPIO dummy driver ID */
  45. 0 /* CX18_HW_CX23418 dummy driver ID */
  46. };
  47. /* This array should match the CX18_HW_ defines */
  48. static const u8 hw_addrs[] = {
  49. 0,
  50. 0,
  51. CX18_CS5345_I2C_ADDR,
  52. 0, /* CX18_HW_GPIO dummy driver ID */
  53. 0, /* CX18_HW_CX23418 dummy driver ID */
  54. };
  55. /* This array should match the CX18_HW_ defines */
  56. /* This might well become a card-specific array */
  57. static const u8 hw_bus[] = {
  58. 0,
  59. 0,
  60. 0,
  61. 0, /* CX18_HW_GPIO dummy driver ID */
  62. 0, /* CX18_HW_CX23418 dummy driver ID */
  63. };
  64. /* This array should match the CX18_HW_ defines */
  65. static const char * const hw_devicenames[] = {
  66. "tuner",
  67. "tveeprom",
  68. "cs5345",
  69. "gpio",
  70. "cx23418",
  71. };
  72. int cx18_i2c_register(struct cx18 *cx, unsigned idx)
  73. {
  74. struct i2c_board_info info;
  75. struct i2c_client *c;
  76. u8 id, bus;
  77. int i;
  78. CX18_DEBUG_I2C("i2c client register\n");
  79. if (idx >= ARRAY_SIZE(hw_driverids) || hw_driverids[idx] == 0)
  80. return -1;
  81. id = hw_driverids[idx];
  82. bus = hw_bus[idx];
  83. memset(&info, 0, sizeof(info));
  84. strlcpy(info.type, hw_devicenames[idx], sizeof(info.type));
  85. info.addr = hw_addrs[idx];
  86. for (i = 0; i < I2C_CLIENTS_MAX; i++)
  87. if (cx->i2c_clients[i] == NULL)
  88. break;
  89. if (i == I2C_CLIENTS_MAX) {
  90. CX18_ERR("insufficient room for new I2C client!\n");
  91. return -ENOMEM;
  92. }
  93. if (id != I2C_DRIVERID_TUNER) {
  94. c = i2c_new_device(&cx->i2c_adap[bus], &info);
  95. if (c->driver == NULL)
  96. i2c_unregister_device(c);
  97. else
  98. cx->i2c_clients[i] = c;
  99. return cx->i2c_clients[i] ? 0 : -ENODEV;
  100. }
  101. /* special tuner handling */
  102. c = i2c_new_probed_device(&cx->i2c_adap[1], &info, cx->card_i2c->radio);
  103. if (c && c->driver == NULL)
  104. i2c_unregister_device(c);
  105. else if (c)
  106. cx->i2c_clients[i++] = c;
  107. c = i2c_new_probed_device(&cx->i2c_adap[1], &info, cx->card_i2c->demod);
  108. if (c && c->driver == NULL)
  109. i2c_unregister_device(c);
  110. else if (c)
  111. cx->i2c_clients[i++] = c;
  112. c = i2c_new_probed_device(&cx->i2c_adap[1], &info, cx->card_i2c->tv);
  113. if (c && c->driver == NULL)
  114. i2c_unregister_device(c);
  115. else if (c)
  116. cx->i2c_clients[i++] = c;
  117. return 0;
  118. }
  119. static int attach_inform(struct i2c_client *client)
  120. {
  121. return 0;
  122. }
  123. static int detach_inform(struct i2c_client *client)
  124. {
  125. int i;
  126. struct cx18 *cx = (struct cx18 *)i2c_get_adapdata(client->adapter);
  127. CX18_DEBUG_I2C("i2c client detach\n");
  128. for (i = 0; i < I2C_CLIENTS_MAX; i++) {
  129. if (cx->i2c_clients[i] == client) {
  130. cx->i2c_clients[i] = NULL;
  131. break;
  132. }
  133. }
  134. CX18_DEBUG_I2C("i2c detach [client=%s,%s]\n",
  135. client->name, (i < I2C_CLIENTS_MAX) ? "ok" : "failed");
  136. return 0;
  137. }
  138. static void cx18_setscl(void *data, int state)
  139. {
  140. struct cx18 *cx = ((struct cx18_i2c_algo_callback_data *)data)->cx;
  141. int bus_index = ((struct cx18_i2c_algo_callback_data *)data)->bus_index;
  142. u32 addr = bus_index ? CX18_REG_I2C_2_WR : CX18_REG_I2C_1_WR;
  143. u32 r = cx18_read_reg(cx, addr);
  144. if (state)
  145. cx18_write_reg(cx, r | SETSCL_BIT, addr);
  146. else
  147. cx18_write_reg(cx, r & ~SETSCL_BIT, addr);
  148. }
  149. static void cx18_setsda(void *data, int state)
  150. {
  151. struct cx18 *cx = ((struct cx18_i2c_algo_callback_data *)data)->cx;
  152. int bus_index = ((struct cx18_i2c_algo_callback_data *)data)->bus_index;
  153. u32 addr = bus_index ? CX18_REG_I2C_2_WR : CX18_REG_I2C_1_WR;
  154. u32 r = cx18_read_reg(cx, addr);
  155. if (state)
  156. cx18_write_reg(cx, r | SETSDL_BIT, addr);
  157. else
  158. cx18_write_reg(cx, r & ~SETSDL_BIT, addr);
  159. }
  160. static int cx18_getscl(void *data)
  161. {
  162. struct cx18 *cx = ((struct cx18_i2c_algo_callback_data *)data)->cx;
  163. int bus_index = ((struct cx18_i2c_algo_callback_data *)data)->bus_index;
  164. u32 addr = bus_index ? CX18_REG_I2C_2_RD : CX18_REG_I2C_1_RD;
  165. return cx18_read_reg(cx, addr) & GETSCL_BIT;
  166. }
  167. static int cx18_getsda(void *data)
  168. {
  169. struct cx18 *cx = ((struct cx18_i2c_algo_callback_data *)data)->cx;
  170. int bus_index = ((struct cx18_i2c_algo_callback_data *)data)->bus_index;
  171. u32 addr = bus_index ? CX18_REG_I2C_2_RD : CX18_REG_I2C_1_RD;
  172. return cx18_read_reg(cx, addr) & GETSDL_BIT;
  173. }
  174. /* template for i2c-bit-algo */
  175. static struct i2c_adapter cx18_i2c_adap_template = {
  176. .name = "cx18 i2c driver",
  177. .id = I2C_HW_B_CX2341X,
  178. .algo = NULL, /* set by i2c-algo-bit */
  179. .algo_data = NULL, /* filled from template */
  180. .client_register = attach_inform,
  181. .client_unregister = detach_inform,
  182. .owner = THIS_MODULE,
  183. };
  184. #define CX18_SCL_PERIOD (10) /* usecs. 10 usec is period for a 100 KHz clock */
  185. #define CX18_ALGO_BIT_TIMEOUT (2) /* seconds */
  186. static struct i2c_algo_bit_data cx18_i2c_algo_template = {
  187. .setsda = cx18_setsda,
  188. .setscl = cx18_setscl,
  189. .getsda = cx18_getsda,
  190. .getscl = cx18_getscl,
  191. .udelay = CX18_SCL_PERIOD/2, /* 1/2 clock period in usec*/
  192. .timeout = CX18_ALGO_BIT_TIMEOUT*HZ /* jiffies */
  193. };
  194. static struct i2c_client cx18_i2c_client_template = {
  195. .name = "cx18 internal",
  196. };
  197. int cx18_call_i2c_client(struct cx18 *cx, int addr, unsigned cmd, void *arg)
  198. {
  199. struct i2c_client *client;
  200. int retval;
  201. int i;
  202. CX18_DEBUG_I2C("call_i2c_client addr=%02x\n", addr);
  203. for (i = 0; i < I2C_CLIENTS_MAX; i++) {
  204. client = cx->i2c_clients[i];
  205. if (client == NULL || client->driver == NULL ||
  206. client->driver->command == NULL)
  207. continue;
  208. if (addr == client->addr) {
  209. retval = client->driver->command(client, cmd, arg);
  210. return retval;
  211. }
  212. }
  213. if (cmd != VIDIOC_G_CHIP_IDENT)
  214. CX18_ERR("i2c addr 0x%02x not found for cmd 0x%x!\n",
  215. addr, cmd);
  216. return -ENODEV;
  217. }
  218. /* Find the i2c device based on the driver ID and return
  219. its i2c address or -ENODEV if no matching device was found. */
  220. static int cx18_i2c_id_addr(struct cx18 *cx, u32 id)
  221. {
  222. struct i2c_client *client;
  223. int retval = -ENODEV;
  224. int i;
  225. for (i = 0; i < I2C_CLIENTS_MAX; i++) {
  226. client = cx->i2c_clients[i];
  227. if (client == NULL || client->driver == NULL)
  228. continue;
  229. if (id == client->driver->id) {
  230. retval = client->addr;
  231. break;
  232. }
  233. }
  234. return retval;
  235. }
  236. /* Find the i2c device name matching the DRIVERID */
  237. static const char *cx18_i2c_id_name(u32 id)
  238. {
  239. int i;
  240. for (i = 0; i < ARRAY_SIZE(hw_driverids); i++)
  241. if (hw_driverids[i] == id)
  242. return hw_devicenames[i];
  243. return "unknown device";
  244. }
  245. /* Find the i2c device name matching the CX18_HW_ flag */
  246. static const char *cx18_i2c_hw_name(u32 hw)
  247. {
  248. int i;
  249. for (i = 0; i < ARRAY_SIZE(hw_driverids); i++)
  250. if (1 << i == hw)
  251. return hw_devicenames[i];
  252. return "unknown device";
  253. }
  254. /* Find the i2c device matching the CX18_HW_ flag and return
  255. its i2c address or -ENODEV if no matching device was found. */
  256. int cx18_i2c_hw_addr(struct cx18 *cx, u32 hw)
  257. {
  258. int i;
  259. for (i = 0; i < ARRAY_SIZE(hw_driverids); i++)
  260. if (1 << i == hw)
  261. return cx18_i2c_id_addr(cx, hw_driverids[i]);
  262. return -ENODEV;
  263. }
  264. /* Calls i2c device based on CX18_HW_ flag. If hw == 0, then do nothing.
  265. If hw == CX18_HW_GPIO then call the gpio handler. */
  266. int cx18_i2c_hw(struct cx18 *cx, u32 hw, unsigned int cmd, void *arg)
  267. {
  268. int addr;
  269. if (hw == 0)
  270. return 0;
  271. if (hw == CX18_HW_GPIO)
  272. return cx18_gpio(cx, cmd, arg);
  273. if (hw == CX18_HW_CX23418)
  274. return cx18_av_cmd(cx, cmd, arg);
  275. addr = cx18_i2c_hw_addr(cx, hw);
  276. if (addr < 0) {
  277. CX18_ERR("i2c hardware 0x%08x (%s) not found for cmd 0x%x!\n",
  278. hw, cx18_i2c_hw_name(hw), cmd);
  279. return addr;
  280. }
  281. return cx18_call_i2c_client(cx, addr, cmd, arg);
  282. }
  283. /* Calls i2c device based on I2C driver ID. */
  284. int cx18_i2c_id(struct cx18 *cx, u32 id, unsigned int cmd, void *arg)
  285. {
  286. int addr;
  287. addr = cx18_i2c_id_addr(cx, id);
  288. if (addr < 0) {
  289. if (cmd != VIDIOC_G_CHIP_IDENT)
  290. CX18_ERR("i2c ID 0x%08x (%s) not found for cmd 0x%x!\n",
  291. id, cx18_i2c_id_name(id), cmd);
  292. return addr;
  293. }
  294. return cx18_call_i2c_client(cx, addr, cmd, arg);
  295. }
  296. /* broadcast cmd for all I2C clients and for the gpio subsystem */
  297. void cx18_call_i2c_clients(struct cx18 *cx, unsigned int cmd, void *arg)
  298. {
  299. if (cx->i2c_adap[0].algo == NULL || cx->i2c_adap[1].algo == NULL) {
  300. CX18_ERR("adapter is not set\n");
  301. return;
  302. }
  303. cx18_av_cmd(cx, cmd, arg);
  304. i2c_clients_command(&cx->i2c_adap[0], cmd, arg);
  305. i2c_clients_command(&cx->i2c_adap[1], cmd, arg);
  306. if (cx->hw_flags & CX18_HW_GPIO)
  307. cx18_gpio(cx, cmd, arg);
  308. }
  309. /* init + register i2c algo-bit adapter */
  310. int init_cx18_i2c(struct cx18 *cx)
  311. {
  312. int i;
  313. CX18_DEBUG_I2C("i2c init\n");
  314. /* Sanity checks for the I2C hardware arrays. They must be the
  315. * same size and GPIO/CX23418 must be the last entries.
  316. */
  317. if (ARRAY_SIZE(hw_driverids) != ARRAY_SIZE(hw_addrs) ||
  318. ARRAY_SIZE(hw_devicenames) != ARRAY_SIZE(hw_addrs) ||
  319. CX18_HW_GPIO != (1 << (ARRAY_SIZE(hw_addrs) - 2)) ||
  320. CX18_HW_CX23418 != (1 << (ARRAY_SIZE(hw_addrs) - 1)) ||
  321. hw_driverids[ARRAY_SIZE(hw_addrs) - 1]) {
  322. CX18_ERR("Mismatched I2C hardware arrays\n");
  323. return -ENODEV;
  324. }
  325. for (i = 0; i < 2; i++) {
  326. memcpy(&cx->i2c_adap[i], &cx18_i2c_adap_template,
  327. sizeof(struct i2c_adapter));
  328. memcpy(&cx->i2c_algo[i], &cx18_i2c_algo_template,
  329. sizeof(struct i2c_algo_bit_data));
  330. cx->i2c_algo_cb_data[i].cx = cx;
  331. cx->i2c_algo_cb_data[i].bus_index = i;
  332. cx->i2c_algo[i].data = &cx->i2c_algo_cb_data[i];
  333. cx->i2c_adap[i].algo_data = &cx->i2c_algo[i];
  334. sprintf(cx->i2c_adap[i].name + strlen(cx->i2c_adap[i].name),
  335. " #%d-%d", cx->num, i);
  336. i2c_set_adapdata(&cx->i2c_adap[i], cx);
  337. memcpy(&cx->i2c_client[i], &cx18_i2c_client_template,
  338. sizeof(struct i2c_client));
  339. sprintf(cx->i2c_client[i].name +
  340. strlen(cx->i2c_client[i].name), "%d", i);
  341. cx->i2c_client[i].adapter = &cx->i2c_adap[i];
  342. cx->i2c_adap[i].dev.parent = &cx->dev->dev;
  343. }
  344. if (cx18_read_reg(cx, CX18_REG_I2C_2_WR) != 0x0003c02f) {
  345. /* Reset/Unreset I2C hardware block */
  346. /* Clock select 220MHz */
  347. cx18_write_reg_expect(cx, 0x10000000, 0xc71004,
  348. 0x00000000, 0x10001000);
  349. /* Clock Enable */
  350. cx18_write_reg_expect(cx, 0x10001000, 0xc71024,
  351. 0x00001000, 0x10001000);
  352. }
  353. /* courtesy of Steven Toth <stoth@hauppauge.com> */
  354. cx18_write_reg_expect(cx, 0x00c00000, 0xc7001c, 0x00000000, 0x00c000c0);
  355. mdelay(10);
  356. cx18_write_reg_expect(cx, 0x00c000c0, 0xc7001c, 0x000000c0, 0x00c000c0);
  357. mdelay(10);
  358. cx18_write_reg_expect(cx, 0x00c00000, 0xc7001c, 0x00000000, 0x00c000c0);
  359. mdelay(10);
  360. /* Set to edge-triggered intrs. */
  361. cx18_write_reg(cx, 0x00c00000, 0xc730c8);
  362. /* Clear any stale intrs */
  363. cx18_write_reg_expect(cx, HW2_I2C1_INT|HW2_I2C2_INT, HW2_INT_CLR_STATUS,
  364. ~(HW2_I2C1_INT|HW2_I2C2_INT), HW2_I2C1_INT|HW2_I2C2_INT);
  365. /* Hw I2C1 Clock Freq ~100kHz */
  366. cx18_write_reg(cx, 0x00021c0f & ~4, CX18_REG_I2C_1_WR);
  367. cx18_setscl(&cx->i2c_algo_cb_data[0], 1);
  368. cx18_setsda(&cx->i2c_algo_cb_data[0], 1);
  369. /* Hw I2C2 Clock Freq ~100kHz */
  370. cx18_write_reg(cx, 0x00021c0f & ~4, CX18_REG_I2C_2_WR);
  371. cx18_setscl(&cx->i2c_algo_cb_data[1], 1);
  372. cx18_setsda(&cx->i2c_algo_cb_data[1], 1);
  373. cx18_reset_i2c_slaves_gpio(cx);
  374. return i2c_bit_add_bus(&cx->i2c_adap[0]) ||
  375. i2c_bit_add_bus(&cx->i2c_adap[1]);
  376. }
  377. void exit_cx18_i2c(struct cx18 *cx)
  378. {
  379. int i;
  380. CX18_DEBUG_I2C("i2c exit\n");
  381. cx18_write_reg(cx, cx18_read_reg(cx, CX18_REG_I2C_1_WR) | 4,
  382. CX18_REG_I2C_1_WR);
  383. cx18_write_reg(cx, cx18_read_reg(cx, CX18_REG_I2C_2_WR) | 4,
  384. CX18_REG_I2C_2_WR);
  385. for (i = 0; i < 2; i++) {
  386. i2c_del_adapter(&cx->i2c_adap[i]);
  387. }
  388. }
  389. /*
  390. Hauppauge HVR1600 should have:
  391. 32 cx24227
  392. 98 unknown
  393. a0 eeprom
  394. c2 tuner
  395. e? zilog ir
  396. */