blob: 7fc64aef1ef7adf50b717a7318cddcb3ca662a32 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Mauro Carvalho Chehab53c4e952007-03-29 08:42:30 -03002
Linus Torvalds1da177e2005-04-16 15:20:36 -07003/*
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -02004 *
5 * cx88-i2c.c -- all the i2c code is here
6 *
7 * Copyright (C) 1996,97,98 Ralph Metzler (rjkm@thp.uni-koeln.de)
8 * & Marcus Metzler (mocm@thp.uni-koeln.de)
9 * (c) 2002 Yurij Sysoev <yurij@naturesoft.net>
10 * (c) 1999-2003 Gerd Knorr <kraxel@bytesex.org>
Mauro Carvalho Chehab32590812018-04-25 05:34:48 -040011 * (c) 2005 Mauro Carvalho Chehab <mchehab@kernel.org>
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -020012 * - Multituner support and i2c address binding
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -020013 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -020015#include "cx88.h"
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/init.h>
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -020018#include <linux/io.h>
19#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Michael Krufky5e453dc2006-01-09 15:32:31 -020021#include <media/v4l2-common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030023static unsigned int i2c_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070024module_param(i2c_debug, int, 0644);
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -020025MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030027static unsigned int i2c_scan;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028module_param(i2c_scan, int, 0444);
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -020029MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Mauro Carvalho Chehab141276b2006-09-06 19:04:28 -030031static unsigned int i2c_udelay = 5;
32module_param(i2c_udelay, int, 0644);
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -020033MODULE_PARM_DESC(i2c_udelay,
34 "i2c delay at insmod time, in usecs (should be 5 or higher). Lower value means higher bus speed.");
Mauro Carvalho Chehab141276b2006-09-06 19:04:28 -030035
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -020036#define dprintk(level, fmt, arg...) do { \
37 if (i2c_debug >= level) \
38 printk(KERN_DEBUG pr_fmt("%s: i2c:" fmt), \
39 __func__, ##arg); \
40} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42/* ----------------------------------------------------------------------- */
43
Adrian Bunk408b6642005-05-01 08:59:29 -070044static void cx8800_bit_setscl(void *data, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
46 struct cx88_core *core = data;
47
48 if (state)
49 core->i2c_state |= 0x02;
50 else
51 core->i2c_state &= ~0x02;
52 cx_write(MO_I2C, core->i2c_state);
53 cx_read(MO_I2C);
54}
55
Adrian Bunk408b6642005-05-01 08:59:29 -070056static void cx8800_bit_setsda(void *data, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
58 struct cx88_core *core = data;
59
60 if (state)
61 core->i2c_state |= 0x01;
62 else
63 core->i2c_state &= ~0x01;
64 cx_write(MO_I2C, core->i2c_state);
65 cx_read(MO_I2C);
66}
67
68static int cx8800_bit_getscl(void *data)
69{
70 struct cx88_core *core = data;
71 u32 state;
72
73 state = cx_read(MO_I2C);
74 return state & 0x02 ? 1 : 0;
75}
76
77static int cx8800_bit_getsda(void *data)
78{
79 struct cx88_core *core = data;
80 u32 state;
81
82 state = cx_read(MO_I2C);
83 return state & 0x01;
84}
85
86/* ----------------------------------------------------------------------- */
87
Jean Delvare7e520d02007-07-01 18:37:51 -030088static const struct i2c_algo_bit_data cx8800_i2c_algo_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 .setsda = cx8800_bit_setsda,
90 .setscl = cx8800_bit_setscl,
91 .getsda = cx8800_bit_getsda,
92 .getscl = cx8800_bit_getscl,
93 .udelay = 16,
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 .timeout = 200,
95};
96
97/* ----------------------------------------------------------------------- */
98
lawrence rust2e4e98e2010-08-25 09:50:20 -030099static const char * const i2c_devs[128] = {
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200100 [0x1c >> 1] = "lgdt330x",
101 [0x86 >> 1] = "tda9887/cx22702",
102 [0xa0 >> 1] = "eeprom",
103 [0xc0 >> 1] = "tuner (analog)",
104 [0xc2 >> 1] = "tuner (analog/dvb)",
105 [0xc8 >> 1] = "xc5000",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106};
107
lawrence rust2e4e98e2010-08-25 09:50:20 -0300108static void do_i2c_scan(const char *name, struct i2c_client *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
110 unsigned char buf;
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200111 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Mauro Carvalho Chehab53c4e952007-03-29 08:42:30 -0300113 for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 c->addr = i;
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200115 rc = i2c_master_recv(c, &buf, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 if (rc < 0)
117 continue;
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -0200118 pr_info("i2c scan: found device @ 0x%x [%s]\n",
119 i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 }
121}
122
Jean Delvarea824f0f2011-11-09 13:27:53 -0300123/* init + register i2c adapter */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124int cx88_i2c_init(struct cx88_core *core, struct pci_dev *pci)
125{
Mauro Carvalho Chehab141276b2006-09-06 19:04:28 -0300126 /* Prevents usage of invalid delay values */
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200127 if (i2c_udelay < 5)
128 i2c_udelay = 5;
Mauro Carvalho Chehab141276b2006-09-06 19:04:28 -0300129
Ezequiel Garciab5237742012-10-23 15:57:19 -0300130 core->i2c_algo = cx8800_i2c_algo_template;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 core->i2c_adap.dev.parent = &pci->dev;
Mauro Carvalho Chehabc0decac2018-09-10 08:19:14 -0400133 strscpy(core->i2c_adap.name, core->name, sizeof(core->i2c_adap.name));
Jean Delvare7e520d02007-07-01 18:37:51 -0300134 core->i2c_adap.owner = THIS_MODULE;
Jean Delvare7e520d02007-07-01 18:37:51 -0300135 core->i2c_algo.udelay = i2c_udelay;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800136 core->i2c_algo.data = core;
Hans Verkuil9467fe12009-03-14 12:40:51 -0300137 i2c_set_adapdata(&core->i2c_adap, &core->v4l2_dev);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800138 core->i2c_adap.algo_data = &core->i2c_algo;
139 core->i2c_client.adapter = &core->i2c_adap;
Mauro Carvalho Chehabc0decac2018-09-10 08:19:14 -0400140 strscpy(core->i2c_client.name, "cx88xx internal", I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200142 cx8800_bit_setscl(core, 1);
143 cx8800_bit_setsda(core, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 core->i2c_rc = i2c_bit_add_bus(&core->i2c_adap);
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200146 if (core->i2c_rc == 0) {
147 static u8 tuner_data[] = {
148 0x0b, 0xdc, 0x86, 0x52 };
149 static struct i2c_msg tuner_msg = {
150 .flags = 0,
151 .addr = 0xc2 >> 1,
152 .buf = tuner_data,
153 .len = 4
154 };
Steven Toth1cbd89d2008-09-22 01:46:26 -0300155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 dprintk(1, "i2c register ok\n");
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200157 switch (core->boardnr) {
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200158 case CX88_BOARD_HAUPPAUGE_HVR1300:
159 case CX88_BOARD_HAUPPAUGE_HVR3000:
160 case CX88_BOARD_HAUPPAUGE_HVR4000:
161 pr_info("i2c init: enabling analog demod on HVR1300/3000/4000 tuner\n");
162 i2c_transfer(core->i2c_client.adapter, &tuner_msg, 1);
163 break;
164 default:
165 break;
Steven Toth1cbd89d2008-09-22 01:46:26 -0300166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 if (i2c_scan)
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200168 do_i2c_scan(core->name, &core->i2c_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 } else
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -0200170 pr_err("i2c register FAILED\n");
Jean Delvarec668f322009-05-13 16:48:50 -0300171
Jean Delvare15ceb6b2010-06-28 12:55:43 -0300172 return core->i2c_rc;
173}