18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * cxd2099.c: Driver for the Sony CXD2099AR Common Interface Controller 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2010-2013 Digital Devices GmbH 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or 88c2ecf20Sopenharmony_ci * modify it under the terms of the GNU General Public License 98c2ecf20Sopenharmony_ci * version 2 only, as published by the Free Software Foundation. 108c2ecf20Sopenharmony_ci * 118c2ecf20Sopenharmony_ci * This program is distributed in the hope that it will be useful, 128c2ecf20Sopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of 138c2ecf20Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 148c2ecf20Sopenharmony_ci * GNU General Public License for more details. 158c2ecf20Sopenharmony_ci */ 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci#include <linux/slab.h> 188c2ecf20Sopenharmony_ci#include <linux/kernel.h> 198c2ecf20Sopenharmony_ci#include <linux/module.h> 208c2ecf20Sopenharmony_ci#include <linux/i2c.h> 218c2ecf20Sopenharmony_ci#include <linux/regmap.h> 228c2ecf20Sopenharmony_ci#include <linux/wait.h> 238c2ecf20Sopenharmony_ci#include <linux/delay.h> 248c2ecf20Sopenharmony_ci#include <linux/mutex.h> 258c2ecf20Sopenharmony_ci#include <linux/io.h> 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ci#include "cxd2099.h" 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_cistatic int buffermode; 308c2ecf20Sopenharmony_cimodule_param(buffermode, int, 0444); 318c2ecf20Sopenharmony_ciMODULE_PARM_DESC(buffermode, "Enable CXD2099AR buffer mode (default: disabled)"); 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_cistatic int read_data(struct dvb_ca_en50221 *ca, int slot, u8 *ebuf, int ecount); 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_cistruct cxd { 368c2ecf20Sopenharmony_ci struct dvb_ca_en50221 en; 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci struct cxd2099_cfg cfg; 398c2ecf20Sopenharmony_ci struct i2c_client *client; 408c2ecf20Sopenharmony_ci struct regmap *regmap; 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci u8 regs[0x23]; 438c2ecf20Sopenharmony_ci u8 lastaddress; 448c2ecf20Sopenharmony_ci u8 clk_reg_f; 458c2ecf20Sopenharmony_ci u8 clk_reg_b; 468c2ecf20Sopenharmony_ci int mode; 478c2ecf20Sopenharmony_ci int ready; 488c2ecf20Sopenharmony_ci int dr; 498c2ecf20Sopenharmony_ci int write_busy; 508c2ecf20Sopenharmony_ci int slot_stat; 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci u8 amem[1024]; 538c2ecf20Sopenharmony_ci int amem_read; 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci int cammode; 568c2ecf20Sopenharmony_ci struct mutex lock; /* device access lock */ 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci u8 rbuf[1028]; 598c2ecf20Sopenharmony_ci u8 wbuf[1028]; 608c2ecf20Sopenharmony_ci}; 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_cistatic int read_block(struct cxd *ci, u8 adr, u8 *data, u16 n) 638c2ecf20Sopenharmony_ci{ 648c2ecf20Sopenharmony_ci int status = 0; 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci if (ci->lastaddress != adr) 678c2ecf20Sopenharmony_ci status = regmap_write(ci->regmap, 0, adr); 688c2ecf20Sopenharmony_ci if (!status) { 698c2ecf20Sopenharmony_ci ci->lastaddress = adr; 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci while (n) { 728c2ecf20Sopenharmony_ci int len = n; 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci if (ci->cfg.max_i2c && len > ci->cfg.max_i2c) 758c2ecf20Sopenharmony_ci len = ci->cfg.max_i2c; 768c2ecf20Sopenharmony_ci status = regmap_raw_read(ci->regmap, 1, data, len); 778c2ecf20Sopenharmony_ci if (status) 788c2ecf20Sopenharmony_ci return status; 798c2ecf20Sopenharmony_ci data += len; 808c2ecf20Sopenharmony_ci n -= len; 818c2ecf20Sopenharmony_ci } 828c2ecf20Sopenharmony_ci } 838c2ecf20Sopenharmony_ci return status; 848c2ecf20Sopenharmony_ci} 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_cistatic int read_reg(struct cxd *ci, u8 reg, u8 *val) 878c2ecf20Sopenharmony_ci{ 888c2ecf20Sopenharmony_ci return read_block(ci, reg, val, 1); 898c2ecf20Sopenharmony_ci} 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_cistatic int read_pccard(struct cxd *ci, u16 address, u8 *data, u8 n) 928c2ecf20Sopenharmony_ci{ 938c2ecf20Sopenharmony_ci int status; 948c2ecf20Sopenharmony_ci u8 addr[2] = {address & 0xff, address >> 8}; 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ci status = regmap_raw_write(ci->regmap, 2, addr, 2); 978c2ecf20Sopenharmony_ci if (!status) 988c2ecf20Sopenharmony_ci status = regmap_raw_read(ci->regmap, 3, data, n); 998c2ecf20Sopenharmony_ci return status; 1008c2ecf20Sopenharmony_ci} 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_cistatic int write_pccard(struct cxd *ci, u16 address, u8 *data, u8 n) 1038c2ecf20Sopenharmony_ci{ 1048c2ecf20Sopenharmony_ci int status; 1058c2ecf20Sopenharmony_ci u8 addr[2] = {address & 0xff, address >> 8}; 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci status = regmap_raw_write(ci->regmap, 2, addr, 2); 1088c2ecf20Sopenharmony_ci if (!status) { 1098c2ecf20Sopenharmony_ci u8 buf[256]; 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci memcpy(buf, data, n); 1128c2ecf20Sopenharmony_ci status = regmap_raw_write(ci->regmap, 3, buf, n); 1138c2ecf20Sopenharmony_ci } 1148c2ecf20Sopenharmony_ci return status; 1158c2ecf20Sopenharmony_ci} 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_cistatic int read_io(struct cxd *ci, u16 address, unsigned int *val) 1188c2ecf20Sopenharmony_ci{ 1198c2ecf20Sopenharmony_ci int status; 1208c2ecf20Sopenharmony_ci u8 addr[2] = {address & 0xff, address >> 8}; 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci status = regmap_raw_write(ci->regmap, 2, addr, 2); 1238c2ecf20Sopenharmony_ci if (!status) 1248c2ecf20Sopenharmony_ci status = regmap_read(ci->regmap, 3, val); 1258c2ecf20Sopenharmony_ci return status; 1268c2ecf20Sopenharmony_ci} 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_cistatic int write_io(struct cxd *ci, u16 address, u8 val) 1298c2ecf20Sopenharmony_ci{ 1308c2ecf20Sopenharmony_ci int status; 1318c2ecf20Sopenharmony_ci u8 addr[2] = {address & 0xff, address >> 8}; 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci status = regmap_raw_write(ci->regmap, 2, addr, 2); 1348c2ecf20Sopenharmony_ci if (!status) 1358c2ecf20Sopenharmony_ci status = regmap_write(ci->regmap, 3, val); 1368c2ecf20Sopenharmony_ci return status; 1378c2ecf20Sopenharmony_ci} 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_cistatic int write_regm(struct cxd *ci, u8 reg, u8 val, u8 mask) 1408c2ecf20Sopenharmony_ci{ 1418c2ecf20Sopenharmony_ci int status = 0; 1428c2ecf20Sopenharmony_ci unsigned int regval; 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci if (ci->lastaddress != reg) 1458c2ecf20Sopenharmony_ci status = regmap_write(ci->regmap, 0, reg); 1468c2ecf20Sopenharmony_ci if (!status && reg >= 6 && reg <= 8 && mask != 0xff) { 1478c2ecf20Sopenharmony_ci status = regmap_read(ci->regmap, 1, ®val); 1488c2ecf20Sopenharmony_ci ci->regs[reg] = regval; 1498c2ecf20Sopenharmony_ci } 1508c2ecf20Sopenharmony_ci ci->lastaddress = reg; 1518c2ecf20Sopenharmony_ci ci->regs[reg] = (ci->regs[reg] & (~mask)) | val; 1528c2ecf20Sopenharmony_ci if (!status) 1538c2ecf20Sopenharmony_ci status = regmap_write(ci->regmap, 1, ci->regs[reg]); 1548c2ecf20Sopenharmony_ci if (reg == 0x20) 1558c2ecf20Sopenharmony_ci ci->regs[reg] &= 0x7f; 1568c2ecf20Sopenharmony_ci return status; 1578c2ecf20Sopenharmony_ci} 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_cistatic int write_reg(struct cxd *ci, u8 reg, u8 val) 1608c2ecf20Sopenharmony_ci{ 1618c2ecf20Sopenharmony_ci return write_regm(ci, reg, val, 0xff); 1628c2ecf20Sopenharmony_ci} 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_cistatic int write_block(struct cxd *ci, u8 adr, u8 *data, u16 n) 1658c2ecf20Sopenharmony_ci{ 1668c2ecf20Sopenharmony_ci int status = 0; 1678c2ecf20Sopenharmony_ci u8 *buf = ci->wbuf; 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ci if (ci->lastaddress != adr) 1708c2ecf20Sopenharmony_ci status = regmap_write(ci->regmap, 0, adr); 1718c2ecf20Sopenharmony_ci if (status) 1728c2ecf20Sopenharmony_ci return status; 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_ci ci->lastaddress = adr; 1758c2ecf20Sopenharmony_ci while (n) { 1768c2ecf20Sopenharmony_ci int len = n; 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_ci if (ci->cfg.max_i2c && (len + 1 > ci->cfg.max_i2c)) 1798c2ecf20Sopenharmony_ci len = ci->cfg.max_i2c - 1; 1808c2ecf20Sopenharmony_ci memcpy(buf, data, len); 1818c2ecf20Sopenharmony_ci status = regmap_raw_write(ci->regmap, 1, buf, len); 1828c2ecf20Sopenharmony_ci if (status) 1838c2ecf20Sopenharmony_ci return status; 1848c2ecf20Sopenharmony_ci n -= len; 1858c2ecf20Sopenharmony_ci data += len; 1868c2ecf20Sopenharmony_ci } 1878c2ecf20Sopenharmony_ci return status; 1888c2ecf20Sopenharmony_ci} 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_cistatic void set_mode(struct cxd *ci, int mode) 1918c2ecf20Sopenharmony_ci{ 1928c2ecf20Sopenharmony_ci if (mode == ci->mode) 1938c2ecf20Sopenharmony_ci return; 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_ci switch (mode) { 1968c2ecf20Sopenharmony_ci case 0x00: /* IO mem */ 1978c2ecf20Sopenharmony_ci write_regm(ci, 0x06, 0x00, 0x07); 1988c2ecf20Sopenharmony_ci break; 1998c2ecf20Sopenharmony_ci case 0x01: /* ATT mem */ 2008c2ecf20Sopenharmony_ci write_regm(ci, 0x06, 0x02, 0x07); 2018c2ecf20Sopenharmony_ci break; 2028c2ecf20Sopenharmony_ci default: 2038c2ecf20Sopenharmony_ci break; 2048c2ecf20Sopenharmony_ci } 2058c2ecf20Sopenharmony_ci ci->mode = mode; 2068c2ecf20Sopenharmony_ci} 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_cistatic void cam_mode(struct cxd *ci, int mode) 2098c2ecf20Sopenharmony_ci{ 2108c2ecf20Sopenharmony_ci u8 dummy; 2118c2ecf20Sopenharmony_ci 2128c2ecf20Sopenharmony_ci if (mode == ci->cammode) 2138c2ecf20Sopenharmony_ci return; 2148c2ecf20Sopenharmony_ci 2158c2ecf20Sopenharmony_ci switch (mode) { 2168c2ecf20Sopenharmony_ci case 0x00: 2178c2ecf20Sopenharmony_ci write_regm(ci, 0x20, 0x80, 0x80); 2188c2ecf20Sopenharmony_ci break; 2198c2ecf20Sopenharmony_ci case 0x01: 2208c2ecf20Sopenharmony_ci if (!ci->en.read_data) 2218c2ecf20Sopenharmony_ci return; 2228c2ecf20Sopenharmony_ci ci->write_busy = 0; 2238c2ecf20Sopenharmony_ci dev_info(&ci->client->dev, "enable cam buffer mode\n"); 2248c2ecf20Sopenharmony_ci write_reg(ci, 0x0d, 0x00); 2258c2ecf20Sopenharmony_ci write_reg(ci, 0x0e, 0x01); 2268c2ecf20Sopenharmony_ci write_regm(ci, 0x08, 0x40, 0x40); 2278c2ecf20Sopenharmony_ci read_reg(ci, 0x12, &dummy); 2288c2ecf20Sopenharmony_ci write_regm(ci, 0x08, 0x80, 0x80); 2298c2ecf20Sopenharmony_ci break; 2308c2ecf20Sopenharmony_ci default: 2318c2ecf20Sopenharmony_ci break; 2328c2ecf20Sopenharmony_ci } 2338c2ecf20Sopenharmony_ci ci->cammode = mode; 2348c2ecf20Sopenharmony_ci} 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_cistatic int init(struct cxd *ci) 2378c2ecf20Sopenharmony_ci{ 2388c2ecf20Sopenharmony_ci int status; 2398c2ecf20Sopenharmony_ci 2408c2ecf20Sopenharmony_ci mutex_lock(&ci->lock); 2418c2ecf20Sopenharmony_ci ci->mode = -1; 2428c2ecf20Sopenharmony_ci do { 2438c2ecf20Sopenharmony_ci status = write_reg(ci, 0x00, 0x00); 2448c2ecf20Sopenharmony_ci if (status < 0) 2458c2ecf20Sopenharmony_ci break; 2468c2ecf20Sopenharmony_ci status = write_reg(ci, 0x01, 0x00); 2478c2ecf20Sopenharmony_ci if (status < 0) 2488c2ecf20Sopenharmony_ci break; 2498c2ecf20Sopenharmony_ci status = write_reg(ci, 0x02, 0x10); 2508c2ecf20Sopenharmony_ci if (status < 0) 2518c2ecf20Sopenharmony_ci break; 2528c2ecf20Sopenharmony_ci status = write_reg(ci, 0x03, 0x00); 2538c2ecf20Sopenharmony_ci if (status < 0) 2548c2ecf20Sopenharmony_ci break; 2558c2ecf20Sopenharmony_ci status = write_reg(ci, 0x05, 0xFF); 2568c2ecf20Sopenharmony_ci if (status < 0) 2578c2ecf20Sopenharmony_ci break; 2588c2ecf20Sopenharmony_ci status = write_reg(ci, 0x06, 0x1F); 2598c2ecf20Sopenharmony_ci if (status < 0) 2608c2ecf20Sopenharmony_ci break; 2618c2ecf20Sopenharmony_ci status = write_reg(ci, 0x07, 0x1F); 2628c2ecf20Sopenharmony_ci if (status < 0) 2638c2ecf20Sopenharmony_ci break; 2648c2ecf20Sopenharmony_ci status = write_reg(ci, 0x08, 0x28); 2658c2ecf20Sopenharmony_ci if (status < 0) 2668c2ecf20Sopenharmony_ci break; 2678c2ecf20Sopenharmony_ci status = write_reg(ci, 0x14, 0x20); 2688c2ecf20Sopenharmony_ci if (status < 0) 2698c2ecf20Sopenharmony_ci break; 2708c2ecf20Sopenharmony_ci 2718c2ecf20Sopenharmony_ci /* TOSTRT = 8, Mode B (gated clock), falling Edge, 2728c2ecf20Sopenharmony_ci * Serial, POL=HIGH, MSB 2738c2ecf20Sopenharmony_ci */ 2748c2ecf20Sopenharmony_ci status = write_reg(ci, 0x0A, 0xA7); 2758c2ecf20Sopenharmony_ci if (status < 0) 2768c2ecf20Sopenharmony_ci break; 2778c2ecf20Sopenharmony_ci 2788c2ecf20Sopenharmony_ci status = write_reg(ci, 0x0B, 0x33); 2798c2ecf20Sopenharmony_ci if (status < 0) 2808c2ecf20Sopenharmony_ci break; 2818c2ecf20Sopenharmony_ci status = write_reg(ci, 0x0C, 0x33); 2828c2ecf20Sopenharmony_ci if (status < 0) 2838c2ecf20Sopenharmony_ci break; 2848c2ecf20Sopenharmony_ci 2858c2ecf20Sopenharmony_ci status = write_regm(ci, 0x14, 0x00, 0x0F); 2868c2ecf20Sopenharmony_ci if (status < 0) 2878c2ecf20Sopenharmony_ci break; 2888c2ecf20Sopenharmony_ci status = write_reg(ci, 0x15, ci->clk_reg_b); 2898c2ecf20Sopenharmony_ci if (status < 0) 2908c2ecf20Sopenharmony_ci break; 2918c2ecf20Sopenharmony_ci status = write_regm(ci, 0x16, 0x00, 0x0F); 2928c2ecf20Sopenharmony_ci if (status < 0) 2938c2ecf20Sopenharmony_ci break; 2948c2ecf20Sopenharmony_ci status = write_reg(ci, 0x17, ci->clk_reg_f); 2958c2ecf20Sopenharmony_ci if (status < 0) 2968c2ecf20Sopenharmony_ci break; 2978c2ecf20Sopenharmony_ci 2988c2ecf20Sopenharmony_ci if (ci->cfg.clock_mode == 2) { 2998c2ecf20Sopenharmony_ci /* bitrate*2^13/ 72000 */ 3008c2ecf20Sopenharmony_ci u32 reg = ((ci->cfg.bitrate << 13) + 71999) / 72000; 3018c2ecf20Sopenharmony_ci 3028c2ecf20Sopenharmony_ci if (ci->cfg.polarity) { 3038c2ecf20Sopenharmony_ci status = write_reg(ci, 0x09, 0x6f); 3048c2ecf20Sopenharmony_ci if (status < 0) 3058c2ecf20Sopenharmony_ci break; 3068c2ecf20Sopenharmony_ci } else { 3078c2ecf20Sopenharmony_ci status = write_reg(ci, 0x09, 0x6d); 3088c2ecf20Sopenharmony_ci if (status < 0) 3098c2ecf20Sopenharmony_ci break; 3108c2ecf20Sopenharmony_ci } 3118c2ecf20Sopenharmony_ci status = write_reg(ci, 0x20, 0x08); 3128c2ecf20Sopenharmony_ci if (status < 0) 3138c2ecf20Sopenharmony_ci break; 3148c2ecf20Sopenharmony_ci status = write_reg(ci, 0x21, (reg >> 8) & 0xff); 3158c2ecf20Sopenharmony_ci if (status < 0) 3168c2ecf20Sopenharmony_ci break; 3178c2ecf20Sopenharmony_ci status = write_reg(ci, 0x22, reg & 0xff); 3188c2ecf20Sopenharmony_ci if (status < 0) 3198c2ecf20Sopenharmony_ci break; 3208c2ecf20Sopenharmony_ci } else if (ci->cfg.clock_mode == 1) { 3218c2ecf20Sopenharmony_ci if (ci->cfg.polarity) { 3228c2ecf20Sopenharmony_ci status = write_reg(ci, 0x09, 0x6f); /* D */ 3238c2ecf20Sopenharmony_ci if (status < 0) 3248c2ecf20Sopenharmony_ci break; 3258c2ecf20Sopenharmony_ci } else { 3268c2ecf20Sopenharmony_ci status = write_reg(ci, 0x09, 0x6d); 3278c2ecf20Sopenharmony_ci if (status < 0) 3288c2ecf20Sopenharmony_ci break; 3298c2ecf20Sopenharmony_ci } 3308c2ecf20Sopenharmony_ci status = write_reg(ci, 0x20, 0x68); 3318c2ecf20Sopenharmony_ci if (status < 0) 3328c2ecf20Sopenharmony_ci break; 3338c2ecf20Sopenharmony_ci status = write_reg(ci, 0x21, 0x00); 3348c2ecf20Sopenharmony_ci if (status < 0) 3358c2ecf20Sopenharmony_ci break; 3368c2ecf20Sopenharmony_ci status = write_reg(ci, 0x22, 0x02); 3378c2ecf20Sopenharmony_ci if (status < 0) 3388c2ecf20Sopenharmony_ci break; 3398c2ecf20Sopenharmony_ci } else { 3408c2ecf20Sopenharmony_ci if (ci->cfg.polarity) { 3418c2ecf20Sopenharmony_ci status = write_reg(ci, 0x09, 0x4f); /* C */ 3428c2ecf20Sopenharmony_ci if (status < 0) 3438c2ecf20Sopenharmony_ci break; 3448c2ecf20Sopenharmony_ci } else { 3458c2ecf20Sopenharmony_ci status = write_reg(ci, 0x09, 0x4d); 3468c2ecf20Sopenharmony_ci if (status < 0) 3478c2ecf20Sopenharmony_ci break; 3488c2ecf20Sopenharmony_ci } 3498c2ecf20Sopenharmony_ci status = write_reg(ci, 0x20, 0x28); 3508c2ecf20Sopenharmony_ci if (status < 0) 3518c2ecf20Sopenharmony_ci break; 3528c2ecf20Sopenharmony_ci status = write_reg(ci, 0x21, 0x00); 3538c2ecf20Sopenharmony_ci if (status < 0) 3548c2ecf20Sopenharmony_ci break; 3558c2ecf20Sopenharmony_ci status = write_reg(ci, 0x22, 0x07); 3568c2ecf20Sopenharmony_ci if (status < 0) 3578c2ecf20Sopenharmony_ci break; 3588c2ecf20Sopenharmony_ci } 3598c2ecf20Sopenharmony_ci 3608c2ecf20Sopenharmony_ci status = write_regm(ci, 0x20, 0x80, 0x80); 3618c2ecf20Sopenharmony_ci if (status < 0) 3628c2ecf20Sopenharmony_ci break; 3638c2ecf20Sopenharmony_ci status = write_regm(ci, 0x03, 0x02, 0x02); 3648c2ecf20Sopenharmony_ci if (status < 0) 3658c2ecf20Sopenharmony_ci break; 3668c2ecf20Sopenharmony_ci status = write_reg(ci, 0x01, 0x04); 3678c2ecf20Sopenharmony_ci if (status < 0) 3688c2ecf20Sopenharmony_ci break; 3698c2ecf20Sopenharmony_ci status = write_reg(ci, 0x00, 0x31); 3708c2ecf20Sopenharmony_ci if (status < 0) 3718c2ecf20Sopenharmony_ci break; 3728c2ecf20Sopenharmony_ci 3738c2ecf20Sopenharmony_ci /* Put TS in bypass */ 3748c2ecf20Sopenharmony_ci status = write_regm(ci, 0x09, 0x08, 0x08); 3758c2ecf20Sopenharmony_ci if (status < 0) 3768c2ecf20Sopenharmony_ci break; 3778c2ecf20Sopenharmony_ci ci->cammode = -1; 3788c2ecf20Sopenharmony_ci cam_mode(ci, 0); 3798c2ecf20Sopenharmony_ci } while (0); 3808c2ecf20Sopenharmony_ci mutex_unlock(&ci->lock); 3818c2ecf20Sopenharmony_ci 3828c2ecf20Sopenharmony_ci return 0; 3838c2ecf20Sopenharmony_ci} 3848c2ecf20Sopenharmony_ci 3858c2ecf20Sopenharmony_cistatic int read_attribute_mem(struct dvb_ca_en50221 *ca, 3868c2ecf20Sopenharmony_ci int slot, int address) 3878c2ecf20Sopenharmony_ci{ 3888c2ecf20Sopenharmony_ci struct cxd *ci = ca->data; 3898c2ecf20Sopenharmony_ci u8 val; 3908c2ecf20Sopenharmony_ci 3918c2ecf20Sopenharmony_ci mutex_lock(&ci->lock); 3928c2ecf20Sopenharmony_ci set_mode(ci, 1); 3938c2ecf20Sopenharmony_ci read_pccard(ci, address, &val, 1); 3948c2ecf20Sopenharmony_ci mutex_unlock(&ci->lock); 3958c2ecf20Sopenharmony_ci return val; 3968c2ecf20Sopenharmony_ci} 3978c2ecf20Sopenharmony_ci 3988c2ecf20Sopenharmony_cistatic int write_attribute_mem(struct dvb_ca_en50221 *ca, int slot, 3998c2ecf20Sopenharmony_ci int address, u8 value) 4008c2ecf20Sopenharmony_ci{ 4018c2ecf20Sopenharmony_ci struct cxd *ci = ca->data; 4028c2ecf20Sopenharmony_ci 4038c2ecf20Sopenharmony_ci mutex_lock(&ci->lock); 4048c2ecf20Sopenharmony_ci set_mode(ci, 1); 4058c2ecf20Sopenharmony_ci write_pccard(ci, address, &value, 1); 4068c2ecf20Sopenharmony_ci mutex_unlock(&ci->lock); 4078c2ecf20Sopenharmony_ci return 0; 4088c2ecf20Sopenharmony_ci} 4098c2ecf20Sopenharmony_ci 4108c2ecf20Sopenharmony_cistatic int read_cam_control(struct dvb_ca_en50221 *ca, 4118c2ecf20Sopenharmony_ci int slot, u8 address) 4128c2ecf20Sopenharmony_ci{ 4138c2ecf20Sopenharmony_ci struct cxd *ci = ca->data; 4148c2ecf20Sopenharmony_ci unsigned int val; 4158c2ecf20Sopenharmony_ci 4168c2ecf20Sopenharmony_ci mutex_lock(&ci->lock); 4178c2ecf20Sopenharmony_ci set_mode(ci, 0); 4188c2ecf20Sopenharmony_ci read_io(ci, address, &val); 4198c2ecf20Sopenharmony_ci mutex_unlock(&ci->lock); 4208c2ecf20Sopenharmony_ci return val; 4218c2ecf20Sopenharmony_ci} 4228c2ecf20Sopenharmony_ci 4238c2ecf20Sopenharmony_cistatic int write_cam_control(struct dvb_ca_en50221 *ca, int slot, 4248c2ecf20Sopenharmony_ci u8 address, u8 value) 4258c2ecf20Sopenharmony_ci{ 4268c2ecf20Sopenharmony_ci struct cxd *ci = ca->data; 4278c2ecf20Sopenharmony_ci 4288c2ecf20Sopenharmony_ci mutex_lock(&ci->lock); 4298c2ecf20Sopenharmony_ci set_mode(ci, 0); 4308c2ecf20Sopenharmony_ci write_io(ci, address, value); 4318c2ecf20Sopenharmony_ci mutex_unlock(&ci->lock); 4328c2ecf20Sopenharmony_ci return 0; 4338c2ecf20Sopenharmony_ci} 4348c2ecf20Sopenharmony_ci 4358c2ecf20Sopenharmony_cistatic int slot_reset(struct dvb_ca_en50221 *ca, int slot) 4368c2ecf20Sopenharmony_ci{ 4378c2ecf20Sopenharmony_ci struct cxd *ci = ca->data; 4388c2ecf20Sopenharmony_ci 4398c2ecf20Sopenharmony_ci if (ci->cammode) 4408c2ecf20Sopenharmony_ci read_data(ca, slot, ci->rbuf, 0); 4418c2ecf20Sopenharmony_ci 4428c2ecf20Sopenharmony_ci mutex_lock(&ci->lock); 4438c2ecf20Sopenharmony_ci cam_mode(ci, 0); 4448c2ecf20Sopenharmony_ci write_reg(ci, 0x00, 0x21); 4458c2ecf20Sopenharmony_ci write_reg(ci, 0x06, 0x1F); 4468c2ecf20Sopenharmony_ci write_reg(ci, 0x00, 0x31); 4478c2ecf20Sopenharmony_ci write_regm(ci, 0x20, 0x80, 0x80); 4488c2ecf20Sopenharmony_ci write_reg(ci, 0x03, 0x02); 4498c2ecf20Sopenharmony_ci ci->ready = 0; 4508c2ecf20Sopenharmony_ci ci->mode = -1; 4518c2ecf20Sopenharmony_ci { 4528c2ecf20Sopenharmony_ci int i; 4538c2ecf20Sopenharmony_ci 4548c2ecf20Sopenharmony_ci for (i = 0; i < 100; i++) { 4558c2ecf20Sopenharmony_ci usleep_range(10000, 11000); 4568c2ecf20Sopenharmony_ci if (ci->ready) 4578c2ecf20Sopenharmony_ci break; 4588c2ecf20Sopenharmony_ci } 4598c2ecf20Sopenharmony_ci } 4608c2ecf20Sopenharmony_ci mutex_unlock(&ci->lock); 4618c2ecf20Sopenharmony_ci return 0; 4628c2ecf20Sopenharmony_ci} 4638c2ecf20Sopenharmony_ci 4648c2ecf20Sopenharmony_cistatic int slot_shutdown(struct dvb_ca_en50221 *ca, int slot) 4658c2ecf20Sopenharmony_ci{ 4668c2ecf20Sopenharmony_ci struct cxd *ci = ca->data; 4678c2ecf20Sopenharmony_ci 4688c2ecf20Sopenharmony_ci dev_dbg(&ci->client->dev, "%s\n", __func__); 4698c2ecf20Sopenharmony_ci if (ci->cammode) 4708c2ecf20Sopenharmony_ci read_data(ca, slot, ci->rbuf, 0); 4718c2ecf20Sopenharmony_ci mutex_lock(&ci->lock); 4728c2ecf20Sopenharmony_ci write_reg(ci, 0x00, 0x21); 4738c2ecf20Sopenharmony_ci write_reg(ci, 0x06, 0x1F); 4748c2ecf20Sopenharmony_ci msleep(300); 4758c2ecf20Sopenharmony_ci 4768c2ecf20Sopenharmony_ci write_regm(ci, 0x09, 0x08, 0x08); 4778c2ecf20Sopenharmony_ci write_regm(ci, 0x20, 0x80, 0x80); /* Reset CAM Mode */ 4788c2ecf20Sopenharmony_ci write_regm(ci, 0x06, 0x07, 0x07); /* Clear IO Mode */ 4798c2ecf20Sopenharmony_ci 4808c2ecf20Sopenharmony_ci ci->mode = -1; 4818c2ecf20Sopenharmony_ci ci->write_busy = 0; 4828c2ecf20Sopenharmony_ci mutex_unlock(&ci->lock); 4838c2ecf20Sopenharmony_ci return 0; 4848c2ecf20Sopenharmony_ci} 4858c2ecf20Sopenharmony_ci 4868c2ecf20Sopenharmony_cistatic int slot_ts_enable(struct dvb_ca_en50221 *ca, int slot) 4878c2ecf20Sopenharmony_ci{ 4888c2ecf20Sopenharmony_ci struct cxd *ci = ca->data; 4898c2ecf20Sopenharmony_ci 4908c2ecf20Sopenharmony_ci mutex_lock(&ci->lock); 4918c2ecf20Sopenharmony_ci write_regm(ci, 0x09, 0x00, 0x08); 4928c2ecf20Sopenharmony_ci set_mode(ci, 0); 4938c2ecf20Sopenharmony_ci cam_mode(ci, 1); 4948c2ecf20Sopenharmony_ci mutex_unlock(&ci->lock); 4958c2ecf20Sopenharmony_ci return 0; 4968c2ecf20Sopenharmony_ci} 4978c2ecf20Sopenharmony_ci 4988c2ecf20Sopenharmony_cistatic int campoll(struct cxd *ci) 4998c2ecf20Sopenharmony_ci{ 5008c2ecf20Sopenharmony_ci u8 istat; 5018c2ecf20Sopenharmony_ci 5028c2ecf20Sopenharmony_ci read_reg(ci, 0x04, &istat); 5038c2ecf20Sopenharmony_ci if (!istat) 5048c2ecf20Sopenharmony_ci return 0; 5058c2ecf20Sopenharmony_ci write_reg(ci, 0x05, istat); 5068c2ecf20Sopenharmony_ci 5078c2ecf20Sopenharmony_ci if (istat & 0x40) 5088c2ecf20Sopenharmony_ci ci->dr = 1; 5098c2ecf20Sopenharmony_ci if (istat & 0x20) 5108c2ecf20Sopenharmony_ci ci->write_busy = 0; 5118c2ecf20Sopenharmony_ci 5128c2ecf20Sopenharmony_ci if (istat & 2) { 5138c2ecf20Sopenharmony_ci u8 slotstat; 5148c2ecf20Sopenharmony_ci 5158c2ecf20Sopenharmony_ci read_reg(ci, 0x01, &slotstat); 5168c2ecf20Sopenharmony_ci if (!(2 & slotstat)) { 5178c2ecf20Sopenharmony_ci if (!ci->slot_stat) { 5188c2ecf20Sopenharmony_ci ci->slot_stat |= 5198c2ecf20Sopenharmony_ci DVB_CA_EN50221_POLL_CAM_PRESENT; 5208c2ecf20Sopenharmony_ci write_regm(ci, 0x03, 0x08, 0x08); 5218c2ecf20Sopenharmony_ci } 5228c2ecf20Sopenharmony_ci 5238c2ecf20Sopenharmony_ci } else { 5248c2ecf20Sopenharmony_ci if (ci->slot_stat) { 5258c2ecf20Sopenharmony_ci ci->slot_stat = 0; 5268c2ecf20Sopenharmony_ci write_regm(ci, 0x03, 0x00, 0x08); 5278c2ecf20Sopenharmony_ci dev_info(&ci->client->dev, "NO CAM\n"); 5288c2ecf20Sopenharmony_ci ci->ready = 0; 5298c2ecf20Sopenharmony_ci } 5308c2ecf20Sopenharmony_ci } 5318c2ecf20Sopenharmony_ci if ((istat & 8) && 5328c2ecf20Sopenharmony_ci ci->slot_stat == DVB_CA_EN50221_POLL_CAM_PRESENT) { 5338c2ecf20Sopenharmony_ci ci->ready = 1; 5348c2ecf20Sopenharmony_ci ci->slot_stat |= DVB_CA_EN50221_POLL_CAM_READY; 5358c2ecf20Sopenharmony_ci } 5368c2ecf20Sopenharmony_ci } 5378c2ecf20Sopenharmony_ci return 0; 5388c2ecf20Sopenharmony_ci} 5398c2ecf20Sopenharmony_ci 5408c2ecf20Sopenharmony_cistatic int poll_slot_status(struct dvb_ca_en50221 *ca, int slot, int open) 5418c2ecf20Sopenharmony_ci{ 5428c2ecf20Sopenharmony_ci struct cxd *ci = ca->data; 5438c2ecf20Sopenharmony_ci u8 slotstat; 5448c2ecf20Sopenharmony_ci 5458c2ecf20Sopenharmony_ci mutex_lock(&ci->lock); 5468c2ecf20Sopenharmony_ci campoll(ci); 5478c2ecf20Sopenharmony_ci read_reg(ci, 0x01, &slotstat); 5488c2ecf20Sopenharmony_ci mutex_unlock(&ci->lock); 5498c2ecf20Sopenharmony_ci 5508c2ecf20Sopenharmony_ci return ci->slot_stat; 5518c2ecf20Sopenharmony_ci} 5528c2ecf20Sopenharmony_ci 5538c2ecf20Sopenharmony_cistatic int read_data(struct dvb_ca_en50221 *ca, int slot, u8 *ebuf, int ecount) 5548c2ecf20Sopenharmony_ci{ 5558c2ecf20Sopenharmony_ci struct cxd *ci = ca->data; 5568c2ecf20Sopenharmony_ci u8 msb, lsb; 5578c2ecf20Sopenharmony_ci u16 len; 5588c2ecf20Sopenharmony_ci 5598c2ecf20Sopenharmony_ci mutex_lock(&ci->lock); 5608c2ecf20Sopenharmony_ci campoll(ci); 5618c2ecf20Sopenharmony_ci mutex_unlock(&ci->lock); 5628c2ecf20Sopenharmony_ci 5638c2ecf20Sopenharmony_ci if (!ci->dr) 5648c2ecf20Sopenharmony_ci return 0; 5658c2ecf20Sopenharmony_ci 5668c2ecf20Sopenharmony_ci mutex_lock(&ci->lock); 5678c2ecf20Sopenharmony_ci read_reg(ci, 0x0f, &msb); 5688c2ecf20Sopenharmony_ci read_reg(ci, 0x10, &lsb); 5698c2ecf20Sopenharmony_ci len = ((u16)msb << 8) | lsb; 5708c2ecf20Sopenharmony_ci if (len > ecount || len < 2) { 5718c2ecf20Sopenharmony_ci /* read it anyway or cxd may hang */ 5728c2ecf20Sopenharmony_ci read_block(ci, 0x12, ci->rbuf, len); 5738c2ecf20Sopenharmony_ci mutex_unlock(&ci->lock); 5748c2ecf20Sopenharmony_ci return -EIO; 5758c2ecf20Sopenharmony_ci } 5768c2ecf20Sopenharmony_ci read_block(ci, 0x12, ebuf, len); 5778c2ecf20Sopenharmony_ci ci->dr = 0; 5788c2ecf20Sopenharmony_ci mutex_unlock(&ci->lock); 5798c2ecf20Sopenharmony_ci return len; 5808c2ecf20Sopenharmony_ci} 5818c2ecf20Sopenharmony_ci 5828c2ecf20Sopenharmony_cistatic int write_data(struct dvb_ca_en50221 *ca, int slot, u8 *ebuf, int ecount) 5838c2ecf20Sopenharmony_ci{ 5848c2ecf20Sopenharmony_ci struct cxd *ci = ca->data; 5858c2ecf20Sopenharmony_ci 5868c2ecf20Sopenharmony_ci if (ci->write_busy) 5878c2ecf20Sopenharmony_ci return -EAGAIN; 5888c2ecf20Sopenharmony_ci mutex_lock(&ci->lock); 5898c2ecf20Sopenharmony_ci write_reg(ci, 0x0d, ecount >> 8); 5908c2ecf20Sopenharmony_ci write_reg(ci, 0x0e, ecount & 0xff); 5918c2ecf20Sopenharmony_ci write_block(ci, 0x11, ebuf, ecount); 5928c2ecf20Sopenharmony_ci ci->write_busy = 1; 5938c2ecf20Sopenharmony_ci mutex_unlock(&ci->lock); 5948c2ecf20Sopenharmony_ci return ecount; 5958c2ecf20Sopenharmony_ci} 5968c2ecf20Sopenharmony_ci 5978c2ecf20Sopenharmony_cistatic const struct dvb_ca_en50221 en_templ = { 5988c2ecf20Sopenharmony_ci .read_attribute_mem = read_attribute_mem, 5998c2ecf20Sopenharmony_ci .write_attribute_mem = write_attribute_mem, 6008c2ecf20Sopenharmony_ci .read_cam_control = read_cam_control, 6018c2ecf20Sopenharmony_ci .write_cam_control = write_cam_control, 6028c2ecf20Sopenharmony_ci .slot_reset = slot_reset, 6038c2ecf20Sopenharmony_ci .slot_shutdown = slot_shutdown, 6048c2ecf20Sopenharmony_ci .slot_ts_enable = slot_ts_enable, 6058c2ecf20Sopenharmony_ci .poll_slot_status = poll_slot_status, 6068c2ecf20Sopenharmony_ci .read_data = read_data, 6078c2ecf20Sopenharmony_ci .write_data = write_data, 6088c2ecf20Sopenharmony_ci}; 6098c2ecf20Sopenharmony_ci 6108c2ecf20Sopenharmony_cistatic int cxd2099_probe(struct i2c_client *client, 6118c2ecf20Sopenharmony_ci const struct i2c_device_id *id) 6128c2ecf20Sopenharmony_ci{ 6138c2ecf20Sopenharmony_ci struct cxd *ci; 6148c2ecf20Sopenharmony_ci struct cxd2099_cfg *cfg = client->dev.platform_data; 6158c2ecf20Sopenharmony_ci static const struct regmap_config rm_cfg = { 6168c2ecf20Sopenharmony_ci .reg_bits = 8, 6178c2ecf20Sopenharmony_ci .val_bits = 8, 6188c2ecf20Sopenharmony_ci }; 6198c2ecf20Sopenharmony_ci unsigned int val; 6208c2ecf20Sopenharmony_ci int ret; 6218c2ecf20Sopenharmony_ci 6228c2ecf20Sopenharmony_ci ci = kzalloc(sizeof(*ci), GFP_KERNEL); 6238c2ecf20Sopenharmony_ci if (!ci) { 6248c2ecf20Sopenharmony_ci ret = -ENOMEM; 6258c2ecf20Sopenharmony_ci goto err; 6268c2ecf20Sopenharmony_ci } 6278c2ecf20Sopenharmony_ci 6288c2ecf20Sopenharmony_ci ci->client = client; 6298c2ecf20Sopenharmony_ci memcpy(&ci->cfg, cfg, sizeof(ci->cfg)); 6308c2ecf20Sopenharmony_ci 6318c2ecf20Sopenharmony_ci ci->regmap = regmap_init_i2c(client, &rm_cfg); 6328c2ecf20Sopenharmony_ci if (IS_ERR(ci->regmap)) { 6338c2ecf20Sopenharmony_ci ret = PTR_ERR(ci->regmap); 6348c2ecf20Sopenharmony_ci goto err_kfree; 6358c2ecf20Sopenharmony_ci } 6368c2ecf20Sopenharmony_ci 6378c2ecf20Sopenharmony_ci ret = regmap_read(ci->regmap, 0x00, &val); 6388c2ecf20Sopenharmony_ci if (ret < 0) { 6398c2ecf20Sopenharmony_ci dev_info(&client->dev, "No CXD2099AR detected at 0x%02x\n", 6408c2ecf20Sopenharmony_ci client->addr); 6418c2ecf20Sopenharmony_ci goto err_rmexit; 6428c2ecf20Sopenharmony_ci } 6438c2ecf20Sopenharmony_ci 6448c2ecf20Sopenharmony_ci mutex_init(&ci->lock); 6458c2ecf20Sopenharmony_ci ci->lastaddress = 0xff; 6468c2ecf20Sopenharmony_ci ci->clk_reg_b = 0x4a; 6478c2ecf20Sopenharmony_ci ci->clk_reg_f = 0x1b; 6488c2ecf20Sopenharmony_ci 6498c2ecf20Sopenharmony_ci ci->en = en_templ; 6508c2ecf20Sopenharmony_ci ci->en.data = ci; 6518c2ecf20Sopenharmony_ci init(ci); 6528c2ecf20Sopenharmony_ci dev_info(&client->dev, "Attached CXD2099AR at 0x%02x\n", client->addr); 6538c2ecf20Sopenharmony_ci 6548c2ecf20Sopenharmony_ci *cfg->en = &ci->en; 6558c2ecf20Sopenharmony_ci 6568c2ecf20Sopenharmony_ci if (!buffermode) { 6578c2ecf20Sopenharmony_ci ci->en.read_data = NULL; 6588c2ecf20Sopenharmony_ci ci->en.write_data = NULL; 6598c2ecf20Sopenharmony_ci } else { 6608c2ecf20Sopenharmony_ci dev_info(&client->dev, "Using CXD2099AR buffer mode"); 6618c2ecf20Sopenharmony_ci } 6628c2ecf20Sopenharmony_ci 6638c2ecf20Sopenharmony_ci i2c_set_clientdata(client, ci); 6648c2ecf20Sopenharmony_ci 6658c2ecf20Sopenharmony_ci return 0; 6668c2ecf20Sopenharmony_ci 6678c2ecf20Sopenharmony_cierr_rmexit: 6688c2ecf20Sopenharmony_ci regmap_exit(ci->regmap); 6698c2ecf20Sopenharmony_cierr_kfree: 6708c2ecf20Sopenharmony_ci kfree(ci); 6718c2ecf20Sopenharmony_cierr: 6728c2ecf20Sopenharmony_ci 6738c2ecf20Sopenharmony_ci return ret; 6748c2ecf20Sopenharmony_ci} 6758c2ecf20Sopenharmony_ci 6768c2ecf20Sopenharmony_cistatic int cxd2099_remove(struct i2c_client *client) 6778c2ecf20Sopenharmony_ci{ 6788c2ecf20Sopenharmony_ci struct cxd *ci = i2c_get_clientdata(client); 6798c2ecf20Sopenharmony_ci 6808c2ecf20Sopenharmony_ci regmap_exit(ci->regmap); 6818c2ecf20Sopenharmony_ci kfree(ci); 6828c2ecf20Sopenharmony_ci 6838c2ecf20Sopenharmony_ci return 0; 6848c2ecf20Sopenharmony_ci} 6858c2ecf20Sopenharmony_ci 6868c2ecf20Sopenharmony_cistatic const struct i2c_device_id cxd2099_id[] = { 6878c2ecf20Sopenharmony_ci {"cxd2099", 0}, 6888c2ecf20Sopenharmony_ci {} 6898c2ecf20Sopenharmony_ci}; 6908c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, cxd2099_id); 6918c2ecf20Sopenharmony_ci 6928c2ecf20Sopenharmony_cistatic struct i2c_driver cxd2099_driver = { 6938c2ecf20Sopenharmony_ci .driver = { 6948c2ecf20Sopenharmony_ci .name = "cxd2099", 6958c2ecf20Sopenharmony_ci }, 6968c2ecf20Sopenharmony_ci .probe = cxd2099_probe, 6978c2ecf20Sopenharmony_ci .remove = cxd2099_remove, 6988c2ecf20Sopenharmony_ci .id_table = cxd2099_id, 6998c2ecf20Sopenharmony_ci}; 7008c2ecf20Sopenharmony_ci 7018c2ecf20Sopenharmony_cimodule_i2c_driver(cxd2099_driver); 7028c2ecf20Sopenharmony_ci 7038c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Sony CXD2099AR Common Interface controller driver"); 7048c2ecf20Sopenharmony_ciMODULE_AUTHOR("Ralph Metzler"); 7058c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 706