cx18-i2c.c 12 KB

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