cx18-i2c.c 12 KB

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