Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Mauro Carvalho Chehab | 53c4e95 | 2007-03-29 08:42:30 -0300 | [diff] [blame] | 2 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | /* |
Mauro Carvalho Chehab | 7b61ba8 | 2016-11-16 06:59:49 -0200 | [diff] [blame] | 4 | * |
| 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 Chehab | 3259081 | 2018-04-25 05:34:48 -0400 | [diff] [blame] | 11 | * (c) 2005 Mauro Carvalho Chehab <mchehab@kernel.org> |
Mauro Carvalho Chehab | 7b61ba8 | 2016-11-16 06:59:49 -0200 | [diff] [blame] | 12 | * - Multituner support and i2c address binding |
Mauro Carvalho Chehab | 7b61ba8 | 2016-11-16 06:59:49 -0200 | [diff] [blame] | 13 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | |
Mauro Carvalho Chehab | 65bc2fe | 2016-11-13 10:07:38 -0200 | [diff] [blame] | 15 | #include "cx88.h" |
| 16 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/init.h> |
Mauro Carvalho Chehab | 399426c | 2016-11-19 19:27:30 -0200 | [diff] [blame] | 18 | #include <linux/io.h> |
| 19 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
Michael Krufky | 5e453dc | 2006-01-09 15:32:31 -0200 | [diff] [blame] | 21 | #include <media/v4l2-common.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
Douglas Schilling Landgraf | ff699e6 | 2008-04-22 14:41:48 -0300 | [diff] [blame] | 23 | static unsigned int i2c_debug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | module_param(i2c_debug, int, 0644); |
Mauro Carvalho Chehab | 7b61ba8 | 2016-11-16 06:59:49 -0200 | [diff] [blame] | 25 | MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
Douglas Schilling Landgraf | ff699e6 | 2008-04-22 14:41:48 -0300 | [diff] [blame] | 27 | static unsigned int i2c_scan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | module_param(i2c_scan, int, 0444); |
Mauro Carvalho Chehab | 7b61ba8 | 2016-11-16 06:59:49 -0200 | [diff] [blame] | 29 | MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Mauro Carvalho Chehab | 141276b | 2006-09-06 19:04:28 -0300 | [diff] [blame] | 31 | static unsigned int i2c_udelay = 5; |
| 32 | module_param(i2c_udelay, int, 0644); |
Mauro Carvalho Chehab | 399426c | 2016-11-19 19:27:30 -0200 | [diff] [blame] | 33 | MODULE_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 Chehab | 141276b | 2006-09-06 19:04:28 -0300 | [diff] [blame] | 35 | |
Mauro Carvalho Chehab | 65bc2fe | 2016-11-13 10:07:38 -0200 | [diff] [blame] | 36 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
| 42 | /* ----------------------------------------------------------------------- */ |
| 43 | |
Adrian Bunk | 408b664 | 2005-05-01 08:59:29 -0700 | [diff] [blame] | 44 | static void cx8800_bit_setscl(void *data, int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | { |
| 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 Bunk | 408b664 | 2005-05-01 08:59:29 -0700 | [diff] [blame] | 56 | static void cx8800_bit_setsda(void *data, int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | { |
| 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 | |
| 68 | static 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 | |
| 77 | static 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 Delvare | 7e520d0 | 2007-07-01 18:37:51 -0300 | [diff] [blame] | 88 | static const struct i2c_algo_bit_data cx8800_i2c_algo_template = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | .setsda = cx8800_bit_setsda, |
| 90 | .setscl = cx8800_bit_setscl, |
| 91 | .getsda = cx8800_bit_getsda, |
| 92 | .getscl = cx8800_bit_getscl, |
| 93 | .udelay = 16, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | .timeout = 200, |
| 95 | }; |
| 96 | |
| 97 | /* ----------------------------------------------------------------------- */ |
| 98 | |
lawrence rust | 2e4e98e | 2010-08-25 09:50:20 -0300 | [diff] [blame] | 99 | static const char * const i2c_devs[128] = { |
Mauro Carvalho Chehab | 7b61ba8 | 2016-11-16 06:59:49 -0200 | [diff] [blame] | 100 | [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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | }; |
| 107 | |
lawrence rust | 2e4e98e | 2010-08-25 09:50:20 -0300 | [diff] [blame] | 108 | static void do_i2c_scan(const char *name, struct i2c_client *c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | { |
| 110 | unsigned char buf; |
Mauro Carvalho Chehab | 7b61ba8 | 2016-11-16 06:59:49 -0200 | [diff] [blame] | 111 | int i, rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
Mauro Carvalho Chehab | 53c4e95 | 2007-03-29 08:42:30 -0300 | [diff] [blame] | 113 | for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | c->addr = i; |
Mauro Carvalho Chehab | 7b61ba8 | 2016-11-16 06:59:49 -0200 | [diff] [blame] | 115 | rc = i2c_master_recv(c, &buf, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | if (rc < 0) |
| 117 | continue; |
Mauro Carvalho Chehab | 65bc2fe | 2016-11-13 10:07:38 -0200 | [diff] [blame] | 118 | pr_info("i2c scan: found device @ 0x%x [%s]\n", |
| 119 | i << 1, i2c_devs[i] ? i2c_devs[i] : "???"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
Jean Delvare | a824f0f | 2011-11-09 13:27:53 -0300 | [diff] [blame] | 123 | /* init + register i2c adapter */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | int cx88_i2c_init(struct cx88_core *core, struct pci_dev *pci) |
| 125 | { |
Mauro Carvalho Chehab | 141276b | 2006-09-06 19:04:28 -0300 | [diff] [blame] | 126 | /* Prevents usage of invalid delay values */ |
Mauro Carvalho Chehab | 7b61ba8 | 2016-11-16 06:59:49 -0200 | [diff] [blame] | 127 | if (i2c_udelay < 5) |
| 128 | i2c_udelay = 5; |
Mauro Carvalho Chehab | 141276b | 2006-09-06 19:04:28 -0300 | [diff] [blame] | 129 | |
Ezequiel Garcia | b523774 | 2012-10-23 15:57:19 -0300 | [diff] [blame] | 130 | core->i2c_algo = cx8800_i2c_algo_template; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | core->i2c_adap.dev.parent = &pci->dev; |
Mauro Carvalho Chehab | c0decac | 2018-09-10 08:19:14 -0400 | [diff] [blame] | 133 | strscpy(core->i2c_adap.name, core->name, sizeof(core->i2c_adap.name)); |
Jean Delvare | 7e520d0 | 2007-07-01 18:37:51 -0300 | [diff] [blame] | 134 | core->i2c_adap.owner = THIS_MODULE; |
Jean Delvare | 7e520d0 | 2007-07-01 18:37:51 -0300 | [diff] [blame] | 135 | core->i2c_algo.udelay = i2c_udelay; |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 136 | core->i2c_algo.data = core; |
Hans Verkuil | 9467fe1 | 2009-03-14 12:40:51 -0300 | [diff] [blame] | 137 | i2c_set_adapdata(&core->i2c_adap, &core->v4l2_dev); |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 138 | core->i2c_adap.algo_data = &core->i2c_algo; |
| 139 | core->i2c_client.adapter = &core->i2c_adap; |
Mauro Carvalho Chehab | c0decac | 2018-09-10 08:19:14 -0400 | [diff] [blame] | 140 | strscpy(core->i2c_client.name, "cx88xx internal", I2C_NAME_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
Mauro Carvalho Chehab | 7b61ba8 | 2016-11-16 06:59:49 -0200 | [diff] [blame] | 142 | cx8800_bit_setscl(core, 1); |
| 143 | cx8800_bit_setsda(core, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
| 145 | core->i2c_rc = i2c_bit_add_bus(&core->i2c_adap); |
Mauro Carvalho Chehab | 7b61ba8 | 2016-11-16 06:59:49 -0200 | [diff] [blame] | 146 | 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 Toth | 1cbd89d | 2008-09-22 01:46:26 -0300 | [diff] [blame] | 155 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | dprintk(1, "i2c register ok\n"); |
Mauro Carvalho Chehab | 7b61ba8 | 2016-11-16 06:59:49 -0200 | [diff] [blame] | 157 | switch (core->boardnr) { |
Mauro Carvalho Chehab | 399426c | 2016-11-19 19:27:30 -0200 | [diff] [blame] | 158 | 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 Toth | 1cbd89d | 2008-09-22 01:46:26 -0300 | [diff] [blame] | 166 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | if (i2c_scan) |
Mauro Carvalho Chehab | 7b61ba8 | 2016-11-16 06:59:49 -0200 | [diff] [blame] | 168 | do_i2c_scan(core->name, &core->i2c_client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | } else |
Mauro Carvalho Chehab | 65bc2fe | 2016-11-13 10:07:38 -0200 | [diff] [blame] | 170 | pr_err("i2c register FAILED\n"); |
Jean Delvare | c668f32 | 2009-05-13 16:48:50 -0300 | [diff] [blame] | 171 | |
Jean Delvare | 15ceb6b | 2010-06-28 12:55:43 -0300 | [diff] [blame] | 172 | return core->i2c_rc; |
| 173 | } |