18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Driver for the Chrontel CH7322 CEC Controller 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright 2020 Google LLC. 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci/* 98c2ecf20Sopenharmony_ci * Notes 108c2ecf20Sopenharmony_ci * 118c2ecf20Sopenharmony_ci * - This device powers on in Auto Mode which has limited functionality. This 128c2ecf20Sopenharmony_ci * driver disables Auto Mode when it attaches. 138c2ecf20Sopenharmony_ci * 148c2ecf20Sopenharmony_ci */ 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci#include <linux/cec.h> 178c2ecf20Sopenharmony_ci#include <linux/dmi.h> 188c2ecf20Sopenharmony_ci#include <linux/i2c.h> 198c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 208c2ecf20Sopenharmony_ci#include <linux/module.h> 218c2ecf20Sopenharmony_ci#include <linux/mutex.h> 228c2ecf20Sopenharmony_ci#include <linux/pci.h> 238c2ecf20Sopenharmony_ci#include <linux/regmap.h> 248c2ecf20Sopenharmony_ci#include <media/cec.h> 258c2ecf20Sopenharmony_ci#include <media/cec-notifier.h> 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ci#define CH7322_WRITE 0x00 288c2ecf20Sopenharmony_ci#define CH7322_WRITE_MSENT 0x80 298c2ecf20Sopenharmony_ci#define CH7322_WRITE_BOK 0x40 308c2ecf20Sopenharmony_ci#define CH7322_WRITE_NMASK 0x0f 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci/* Write buffer is 0x01-0x10 */ 338c2ecf20Sopenharmony_ci#define CH7322_WRBUF 0x01 348c2ecf20Sopenharmony_ci#define CH7322_WRBUF_LEN 0x10 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci#define CH7322_READ 0x40 378c2ecf20Sopenharmony_ci#define CH7322_READ_NRDT 0x80 388c2ecf20Sopenharmony_ci#define CH7322_READ_MSENT 0x20 398c2ecf20Sopenharmony_ci#define CH7322_READ_NMASK 0x0f 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci/* Read buffer is 0x41-0x50 */ 428c2ecf20Sopenharmony_ci#define CH7322_RDBUF 0x41 438c2ecf20Sopenharmony_ci#define CH7322_RDBUF_LEN 0x10 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci#define CH7322_MODE 0x11 468c2ecf20Sopenharmony_ci#define CH7322_MODE_AUTO 0x78 478c2ecf20Sopenharmony_ci#define CH7322_MODE_SW 0xb5 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci#define CH7322_RESET 0x12 508c2ecf20Sopenharmony_ci#define CH7322_RESET_RST 0x00 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci#define CH7322_POWER 0x13 538c2ecf20Sopenharmony_ci#define CH7322_POWER_FPD 0x04 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci#define CH7322_CFG0 0x17 568c2ecf20Sopenharmony_ci#define CH7322_CFG0_EOBEN 0x40 578c2ecf20Sopenharmony_ci#define CH7322_CFG0_PEOB 0x20 588c2ecf20Sopenharmony_ci#define CH7322_CFG0_CLRSPP 0x10 598c2ecf20Sopenharmony_ci#define CH7322_CFG0_FLOW 0x08 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ci#define CH7322_CFG1 0x1a 628c2ecf20Sopenharmony_ci#define CH7322_CFG1_STDBYO 0x04 638c2ecf20Sopenharmony_ci#define CH7322_CFG1_HPBP 0x02 648c2ecf20Sopenharmony_ci#define CH7322_CFG1_PIO 0x01 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci#define CH7322_INTCTL 0x1b 678c2ecf20Sopenharmony_ci#define CH7322_INTCTL_INTPB 0x80 688c2ecf20Sopenharmony_ci#define CH7322_INTCTL_STDBY 0x40 698c2ecf20Sopenharmony_ci#define CH7322_INTCTL_HPDFALL 0x20 708c2ecf20Sopenharmony_ci#define CH7322_INTCTL_HPDRISE 0x10 718c2ecf20Sopenharmony_ci#define CH7322_INTCTL_RXMSG 0x08 728c2ecf20Sopenharmony_ci#define CH7322_INTCTL_TXMSG 0x04 738c2ecf20Sopenharmony_ci#define CH7322_INTCTL_NEWPHA 0x02 748c2ecf20Sopenharmony_ci#define CH7322_INTCTL_ERROR 0x01 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci#define CH7322_DVCLKFNH 0x1d 778c2ecf20Sopenharmony_ci#define CH7322_DVCLKFNL 0x1e 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci#define CH7322_CTL 0x31 808c2ecf20Sopenharmony_ci#define CH7322_CTL_FSTDBY 0x80 818c2ecf20Sopenharmony_ci#define CH7322_CTL_PLSEN 0x40 828c2ecf20Sopenharmony_ci#define CH7322_CTL_PLSPB 0x20 838c2ecf20Sopenharmony_ci#define CH7322_CTL_SPADL 0x10 848c2ecf20Sopenharmony_ci#define CH7322_CTL_HINIT 0x08 858c2ecf20Sopenharmony_ci#define CH7322_CTL_WPHYA 0x04 868c2ecf20Sopenharmony_ci#define CH7322_CTL_H1T 0x02 878c2ecf20Sopenharmony_ci#define CH7322_CTL_S1T 0x01 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ci#define CH7322_PAWH 0x32 908c2ecf20Sopenharmony_ci#define CH7322_PAWL 0x33 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci#define CH7322_ADDLW 0x34 938c2ecf20Sopenharmony_ci#define CH7322_ADDLW_MASK 0xf0 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci#define CH7322_ADDLR 0x3d 968c2ecf20Sopenharmony_ci#define CH7322_ADDLR_HPD 0x80 978c2ecf20Sopenharmony_ci#define CH7322_ADDLR_MASK 0x0f 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci#define CH7322_INTDATA 0x3e 1008c2ecf20Sopenharmony_ci#define CH7322_INTDATA_MODE 0x80 1018c2ecf20Sopenharmony_ci#define CH7322_INTDATA_STDBY 0x40 1028c2ecf20Sopenharmony_ci#define CH7322_INTDATA_HPDFALL 0x20 1038c2ecf20Sopenharmony_ci#define CH7322_INTDATA_HPDRISE 0x10 1048c2ecf20Sopenharmony_ci#define CH7322_INTDATA_RXMSG 0x08 1058c2ecf20Sopenharmony_ci#define CH7322_INTDATA_TXMSG 0x04 1068c2ecf20Sopenharmony_ci#define CH7322_INTDATA_NEWPHA 0x02 1078c2ecf20Sopenharmony_ci#define CH7322_INTDATA_ERROR 0x01 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci#define CH7322_EVENT 0x3f 1108c2ecf20Sopenharmony_ci#define CH7322_EVENT_TXERR 0x80 1118c2ecf20Sopenharmony_ci#define CH7322_EVENT_HRST 0x40 1128c2ecf20Sopenharmony_ci#define CH7322_EVENT_HFST 0x20 1138c2ecf20Sopenharmony_ci#define CH7322_EVENT_PHACHG 0x10 1148c2ecf20Sopenharmony_ci#define CH7322_EVENT_ACTST 0x08 1158c2ecf20Sopenharmony_ci#define CH7322_EVENT_PHARDY 0x04 1168c2ecf20Sopenharmony_ci#define CH7322_EVENT_BSOK 0x02 1178c2ecf20Sopenharmony_ci#define CH7322_EVENT_ERRADCF 0x01 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci#define CH7322_DID 0x51 1208c2ecf20Sopenharmony_ci#define CH7322_DID_CH7322 0x5b 1218c2ecf20Sopenharmony_ci#define CH7322_DID_CH7323 0x5f 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci#define CH7322_REVISIONID 0x52 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_ci#define CH7322_PARH 0x53 1268c2ecf20Sopenharmony_ci#define CH7322_PARL 0x54 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ci#define CH7322_IOCFG2 0x75 1298c2ecf20Sopenharmony_ci#define CH7322_IOCFG_CIO 0x80 1308c2ecf20Sopenharmony_ci#define CH7322_IOCFG_IOCFGMASK 0x78 1318c2ecf20Sopenharmony_ci#define CH7322_IOCFG_AUDIO 0x04 1328c2ecf20Sopenharmony_ci#define CH7322_IOCFG_SPAMST 0x02 1338c2ecf20Sopenharmony_ci#define CH7322_IOCFG_SPAMSP 0x01 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci#define CH7322_CTL3 0x7b 1368c2ecf20Sopenharmony_ci#define CH7322_CTL3_SWENA 0x80 1378c2ecf20Sopenharmony_ci#define CH7322_CTL3_FC_INIT 0x40 1388c2ecf20Sopenharmony_ci#define CH7322_CTL3_SML_FL 0x20 1398c2ecf20Sopenharmony_ci#define CH7322_CTL3_SM_RDST 0x10 1408c2ecf20Sopenharmony_ci#define CH7322_CTL3_SPP_CIAH 0x08 1418c2ecf20Sopenharmony_ci#define CH7322_CTL3_SPP_CIAL 0x04 1428c2ecf20Sopenharmony_ci#define CH7322_CTL3_SPP_ACTH 0x02 1438c2ecf20Sopenharmony_ci#define CH7322_CTL3_SPP_ACTL 0x01 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ci/* BOK status means NACK */ 1468c2ecf20Sopenharmony_ci#define CH7322_TX_FLAG_NACK BIT(0) 1478c2ecf20Sopenharmony_ci/* Device will retry automatically */ 1488c2ecf20Sopenharmony_ci#define CH7322_TX_FLAG_RETRY BIT(1) 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_cistruct ch7322 { 1518c2ecf20Sopenharmony_ci struct i2c_client *i2c; 1528c2ecf20Sopenharmony_ci struct regmap *regmap; 1538c2ecf20Sopenharmony_ci struct cec_adapter *cec; 1548c2ecf20Sopenharmony_ci struct mutex mutex; /* device access mutex */ 1558c2ecf20Sopenharmony_ci u8 tx_flags; 1568c2ecf20Sopenharmony_ci}; 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_cistatic const struct regmap_config ch7322_regmap = { 1598c2ecf20Sopenharmony_ci .reg_bits = 8, 1608c2ecf20Sopenharmony_ci .val_bits = 8, 1618c2ecf20Sopenharmony_ci .max_register = 0x7f, 1628c2ecf20Sopenharmony_ci .disable_locking = true, 1638c2ecf20Sopenharmony_ci}; 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_cistatic int ch7322_send_message(struct ch7322 *ch7322, const struct cec_msg *msg) 1668c2ecf20Sopenharmony_ci{ 1678c2ecf20Sopenharmony_ci unsigned int val; 1688c2ecf20Sopenharmony_ci unsigned int len = msg->len; 1698c2ecf20Sopenharmony_ci int ret; 1708c2ecf20Sopenharmony_ci int i; 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_ci WARN_ON(!mutex_is_locked(&ch7322->mutex)); 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_ci if (len > CH7322_WRBUF_LEN || len < 1) 1758c2ecf20Sopenharmony_ci return -EINVAL; 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ci ret = regmap_read(ch7322->regmap, CH7322_WRITE, &val); 1788c2ecf20Sopenharmony_ci if (ret) 1798c2ecf20Sopenharmony_ci return ret; 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci /* Buffer not ready */ 1828c2ecf20Sopenharmony_ci if (!(val & CH7322_WRITE_MSENT)) 1838c2ecf20Sopenharmony_ci return -EBUSY; 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ci if (cec_msg_opcode(msg) == -1 && 1868c2ecf20Sopenharmony_ci cec_msg_initiator(msg) == cec_msg_destination(msg)) { 1878c2ecf20Sopenharmony_ci ch7322->tx_flags = CH7322_TX_FLAG_NACK | CH7322_TX_FLAG_RETRY; 1888c2ecf20Sopenharmony_ci } else if (cec_msg_is_broadcast(msg)) { 1898c2ecf20Sopenharmony_ci ch7322->tx_flags = CH7322_TX_FLAG_NACK; 1908c2ecf20Sopenharmony_ci } else { 1918c2ecf20Sopenharmony_ci ch7322->tx_flags = CH7322_TX_FLAG_RETRY; 1928c2ecf20Sopenharmony_ci } 1938c2ecf20Sopenharmony_ci 1948c2ecf20Sopenharmony_ci ret = regmap_write(ch7322->regmap, CH7322_WRITE, len - 1); 1958c2ecf20Sopenharmony_ci if (ret) 1968c2ecf20Sopenharmony_ci return ret; 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_ci for (i = 0; i < len; i++) { 1998c2ecf20Sopenharmony_ci ret = regmap_write(ch7322->regmap, 2008c2ecf20Sopenharmony_ci CH7322_WRBUF + i, msg->msg[i]); 2018c2ecf20Sopenharmony_ci if (ret) 2028c2ecf20Sopenharmony_ci return ret; 2038c2ecf20Sopenharmony_ci } 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_ci return 0; 2068c2ecf20Sopenharmony_ci} 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_cistatic int ch7322_receive_message(struct ch7322 *ch7322, struct cec_msg *msg) 2098c2ecf20Sopenharmony_ci{ 2108c2ecf20Sopenharmony_ci unsigned int val; 2118c2ecf20Sopenharmony_ci int ret = 0; 2128c2ecf20Sopenharmony_ci int i; 2138c2ecf20Sopenharmony_ci 2148c2ecf20Sopenharmony_ci WARN_ON(!mutex_is_locked(&ch7322->mutex)); 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_ci ret = regmap_read(ch7322->regmap, CH7322_READ, &val); 2178c2ecf20Sopenharmony_ci if (ret) 2188c2ecf20Sopenharmony_ci return ret; 2198c2ecf20Sopenharmony_ci 2208c2ecf20Sopenharmony_ci /* Message not ready */ 2218c2ecf20Sopenharmony_ci if (!(val & CH7322_READ_NRDT)) 2228c2ecf20Sopenharmony_ci return -EIO; 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_ci msg->len = (val & CH7322_READ_NMASK) + 1; 2258c2ecf20Sopenharmony_ci 2268c2ecf20Sopenharmony_ci /* Read entire RDBUF to clear state */ 2278c2ecf20Sopenharmony_ci for (i = 0; i < CH7322_RDBUF_LEN; i++) { 2288c2ecf20Sopenharmony_ci ret = regmap_read(ch7322->regmap, CH7322_RDBUF + i, &val); 2298c2ecf20Sopenharmony_ci if (ret) 2308c2ecf20Sopenharmony_ci return ret; 2318c2ecf20Sopenharmony_ci msg->msg[i] = (u8)val; 2328c2ecf20Sopenharmony_ci } 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_ci return 0; 2358c2ecf20Sopenharmony_ci} 2368c2ecf20Sopenharmony_ci 2378c2ecf20Sopenharmony_cistatic void ch7322_tx_done(struct ch7322 *ch7322) 2388c2ecf20Sopenharmony_ci{ 2398c2ecf20Sopenharmony_ci int ret; 2408c2ecf20Sopenharmony_ci unsigned int val; 2418c2ecf20Sopenharmony_ci u8 status, flags; 2428c2ecf20Sopenharmony_ci 2438c2ecf20Sopenharmony_ci mutex_lock(&ch7322->mutex); 2448c2ecf20Sopenharmony_ci ret = regmap_read(ch7322->regmap, CH7322_WRITE, &val); 2458c2ecf20Sopenharmony_ci flags = ch7322->tx_flags; 2468c2ecf20Sopenharmony_ci mutex_unlock(&ch7322->mutex); 2478c2ecf20Sopenharmony_ci 2488c2ecf20Sopenharmony_ci /* 2498c2ecf20Sopenharmony_ci * The device returns a one-bit OK status which usually means ACK but 2508c2ecf20Sopenharmony_ci * actually means NACK when sending a logical address query or a 2518c2ecf20Sopenharmony_ci * broadcast. 2528c2ecf20Sopenharmony_ci */ 2538c2ecf20Sopenharmony_ci if (ret) 2548c2ecf20Sopenharmony_ci status = CEC_TX_STATUS_ERROR; 2558c2ecf20Sopenharmony_ci else if ((val & CH7322_WRITE_BOK) && (flags & CH7322_TX_FLAG_NACK)) 2568c2ecf20Sopenharmony_ci status = CEC_TX_STATUS_NACK; 2578c2ecf20Sopenharmony_ci else if (val & CH7322_WRITE_BOK) 2588c2ecf20Sopenharmony_ci status = CEC_TX_STATUS_OK; 2598c2ecf20Sopenharmony_ci else if (flags & CH7322_TX_FLAG_NACK) 2608c2ecf20Sopenharmony_ci status = CEC_TX_STATUS_OK; 2618c2ecf20Sopenharmony_ci else 2628c2ecf20Sopenharmony_ci status = CEC_TX_STATUS_NACK; 2638c2ecf20Sopenharmony_ci 2648c2ecf20Sopenharmony_ci if (status == CEC_TX_STATUS_NACK && (flags & CH7322_TX_FLAG_RETRY)) 2658c2ecf20Sopenharmony_ci status |= CEC_TX_STATUS_MAX_RETRIES; 2668c2ecf20Sopenharmony_ci 2678c2ecf20Sopenharmony_ci cec_transmit_attempt_done(ch7322->cec, status); 2688c2ecf20Sopenharmony_ci} 2698c2ecf20Sopenharmony_ci 2708c2ecf20Sopenharmony_cistatic void ch7322_rx_done(struct ch7322 *ch7322) 2718c2ecf20Sopenharmony_ci{ 2728c2ecf20Sopenharmony_ci struct cec_msg msg; 2738c2ecf20Sopenharmony_ci int ret; 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_ci mutex_lock(&ch7322->mutex); 2768c2ecf20Sopenharmony_ci ret = ch7322_receive_message(ch7322, &msg); 2778c2ecf20Sopenharmony_ci mutex_unlock(&ch7322->mutex); 2788c2ecf20Sopenharmony_ci 2798c2ecf20Sopenharmony_ci if (ret) 2808c2ecf20Sopenharmony_ci dev_err(&ch7322->i2c->dev, "cec receive error: %d\n", ret); 2818c2ecf20Sopenharmony_ci else 2828c2ecf20Sopenharmony_ci cec_received_msg(ch7322->cec, &msg); 2838c2ecf20Sopenharmony_ci} 2848c2ecf20Sopenharmony_ci 2858c2ecf20Sopenharmony_ci/* 2868c2ecf20Sopenharmony_ci * This device can either monitor the DDC lines to obtain the physical address 2878c2ecf20Sopenharmony_ci * or it can allow the host to program it. This driver lets the device obtain 2888c2ecf20Sopenharmony_ci * it. 2898c2ecf20Sopenharmony_ci */ 2908c2ecf20Sopenharmony_cistatic void ch7322_phys_addr(struct ch7322 *ch7322) 2918c2ecf20Sopenharmony_ci{ 2928c2ecf20Sopenharmony_ci unsigned int pah, pal; 2938c2ecf20Sopenharmony_ci int ret = 0; 2948c2ecf20Sopenharmony_ci 2958c2ecf20Sopenharmony_ci mutex_lock(&ch7322->mutex); 2968c2ecf20Sopenharmony_ci ret |= regmap_read(ch7322->regmap, CH7322_PARH, &pah); 2978c2ecf20Sopenharmony_ci ret |= regmap_read(ch7322->regmap, CH7322_PARL, &pal); 2988c2ecf20Sopenharmony_ci mutex_unlock(&ch7322->mutex); 2998c2ecf20Sopenharmony_ci 3008c2ecf20Sopenharmony_ci if (ret) 3018c2ecf20Sopenharmony_ci dev_err(&ch7322->i2c->dev, "phys addr error\n"); 3028c2ecf20Sopenharmony_ci else 3038c2ecf20Sopenharmony_ci cec_s_phys_addr(ch7322->cec, pal | (pah << 8), false); 3048c2ecf20Sopenharmony_ci} 3058c2ecf20Sopenharmony_ci 3068c2ecf20Sopenharmony_cistatic irqreturn_t ch7322_irq(int irq, void *dev) 3078c2ecf20Sopenharmony_ci{ 3088c2ecf20Sopenharmony_ci struct ch7322 *ch7322 = dev; 3098c2ecf20Sopenharmony_ci unsigned int data = 0; 3108c2ecf20Sopenharmony_ci 3118c2ecf20Sopenharmony_ci mutex_lock(&ch7322->mutex); 3128c2ecf20Sopenharmony_ci regmap_read(ch7322->regmap, CH7322_INTDATA, &data); 3138c2ecf20Sopenharmony_ci regmap_write(ch7322->regmap, CH7322_INTDATA, data); 3148c2ecf20Sopenharmony_ci mutex_unlock(&ch7322->mutex); 3158c2ecf20Sopenharmony_ci 3168c2ecf20Sopenharmony_ci if (data & CH7322_INTDATA_HPDFALL) 3178c2ecf20Sopenharmony_ci cec_phys_addr_invalidate(ch7322->cec); 3188c2ecf20Sopenharmony_ci 3198c2ecf20Sopenharmony_ci if (data & CH7322_INTDATA_TXMSG) 3208c2ecf20Sopenharmony_ci ch7322_tx_done(ch7322); 3218c2ecf20Sopenharmony_ci 3228c2ecf20Sopenharmony_ci if (data & CH7322_INTDATA_RXMSG) 3238c2ecf20Sopenharmony_ci ch7322_rx_done(ch7322); 3248c2ecf20Sopenharmony_ci 3258c2ecf20Sopenharmony_ci if (data & CH7322_INTDATA_NEWPHA) 3268c2ecf20Sopenharmony_ci ch7322_phys_addr(ch7322); 3278c2ecf20Sopenharmony_ci 3288c2ecf20Sopenharmony_ci if (data & CH7322_INTDATA_ERROR) 3298c2ecf20Sopenharmony_ci dev_dbg(&ch7322->i2c->dev, "unknown error\n"); 3308c2ecf20Sopenharmony_ci 3318c2ecf20Sopenharmony_ci return IRQ_HANDLED; 3328c2ecf20Sopenharmony_ci} 3338c2ecf20Sopenharmony_ci 3348c2ecf20Sopenharmony_ci/* This device is always enabled */ 3358c2ecf20Sopenharmony_cistatic int ch7322_cec_adap_enable(struct cec_adapter *adap, bool enable) 3368c2ecf20Sopenharmony_ci{ 3378c2ecf20Sopenharmony_ci return 0; 3388c2ecf20Sopenharmony_ci} 3398c2ecf20Sopenharmony_ci 3408c2ecf20Sopenharmony_cistatic int ch7322_cec_adap_log_addr(struct cec_adapter *adap, u8 log_addr) 3418c2ecf20Sopenharmony_ci{ 3428c2ecf20Sopenharmony_ci struct ch7322 *ch7322 = cec_get_drvdata(adap); 3438c2ecf20Sopenharmony_ci int ret; 3448c2ecf20Sopenharmony_ci 3458c2ecf20Sopenharmony_ci mutex_lock(&ch7322->mutex); 3468c2ecf20Sopenharmony_ci ret = regmap_update_bits(ch7322->regmap, CH7322_ADDLW, 3478c2ecf20Sopenharmony_ci CH7322_ADDLW_MASK, log_addr << 4); 3488c2ecf20Sopenharmony_ci mutex_unlock(&ch7322->mutex); 3498c2ecf20Sopenharmony_ci 3508c2ecf20Sopenharmony_ci return ret; 3518c2ecf20Sopenharmony_ci} 3528c2ecf20Sopenharmony_ci 3538c2ecf20Sopenharmony_cistatic int ch7322_cec_adap_transmit(struct cec_adapter *adap, u8 attempts, 3548c2ecf20Sopenharmony_ci u32 signal_free_time, struct cec_msg *msg) 3558c2ecf20Sopenharmony_ci{ 3568c2ecf20Sopenharmony_ci struct ch7322 *ch7322 = cec_get_drvdata(adap); 3578c2ecf20Sopenharmony_ci int ret; 3588c2ecf20Sopenharmony_ci 3598c2ecf20Sopenharmony_ci mutex_lock(&ch7322->mutex); 3608c2ecf20Sopenharmony_ci ret = ch7322_send_message(ch7322, msg); 3618c2ecf20Sopenharmony_ci mutex_unlock(&ch7322->mutex); 3628c2ecf20Sopenharmony_ci 3638c2ecf20Sopenharmony_ci return ret; 3648c2ecf20Sopenharmony_ci} 3658c2ecf20Sopenharmony_ci 3668c2ecf20Sopenharmony_cistatic const struct cec_adap_ops ch7322_cec_adap_ops = { 3678c2ecf20Sopenharmony_ci .adap_enable = ch7322_cec_adap_enable, 3688c2ecf20Sopenharmony_ci .adap_log_addr = ch7322_cec_adap_log_addr, 3698c2ecf20Sopenharmony_ci .adap_transmit = ch7322_cec_adap_transmit, 3708c2ecf20Sopenharmony_ci}; 3718c2ecf20Sopenharmony_ci 3728c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_PCI) && IS_ENABLED(CONFIG_DMI) 3738c2ecf20Sopenharmony_ci 3748c2ecf20Sopenharmony_cistruct ch7322_conn_match { 3758c2ecf20Sopenharmony_ci const char *dev_name; 3768c2ecf20Sopenharmony_ci const char *pci_name; 3778c2ecf20Sopenharmony_ci const char *port_name; 3788c2ecf20Sopenharmony_ci}; 3798c2ecf20Sopenharmony_ci 3808c2ecf20Sopenharmony_cistatic struct ch7322_conn_match google_endeavour[] = { 3818c2ecf20Sopenharmony_ci { "i2c-PRP0001:00", "0000:00:02.0", "Port B" }, 3828c2ecf20Sopenharmony_ci { "i2c-PRP0001:01", "0000:00:02.0", "Port C" }, 3838c2ecf20Sopenharmony_ci { }, 3848c2ecf20Sopenharmony_ci}; 3858c2ecf20Sopenharmony_ci 3868c2ecf20Sopenharmony_cistatic const struct dmi_system_id ch7322_dmi_table[] = { 3878c2ecf20Sopenharmony_ci { 3888c2ecf20Sopenharmony_ci .matches = { 3898c2ecf20Sopenharmony_ci DMI_MATCH(DMI_BOARD_VENDOR, "Google"), 3908c2ecf20Sopenharmony_ci DMI_MATCH(DMI_BOARD_NAME, "Endeavour"), 3918c2ecf20Sopenharmony_ci }, 3928c2ecf20Sopenharmony_ci .driver_data = google_endeavour, 3938c2ecf20Sopenharmony_ci }, 3948c2ecf20Sopenharmony_ci { }, 3958c2ecf20Sopenharmony_ci}; 3968c2ecf20Sopenharmony_ci 3978c2ecf20Sopenharmony_ci/* Make a best-effort attempt to locate a matching HDMI port */ 3988c2ecf20Sopenharmony_cistatic int ch7322_get_port(struct i2c_client *client, 3998c2ecf20Sopenharmony_ci struct device **dev, 4008c2ecf20Sopenharmony_ci const char **port) 4018c2ecf20Sopenharmony_ci{ 4028c2ecf20Sopenharmony_ci const struct dmi_system_id *system; 4038c2ecf20Sopenharmony_ci const struct ch7322_conn_match *conn; 4048c2ecf20Sopenharmony_ci 4058c2ecf20Sopenharmony_ci *dev = NULL; 4068c2ecf20Sopenharmony_ci *port = NULL; 4078c2ecf20Sopenharmony_ci 4088c2ecf20Sopenharmony_ci system = dmi_first_match(ch7322_dmi_table); 4098c2ecf20Sopenharmony_ci if (!system) 4108c2ecf20Sopenharmony_ci return 0; 4118c2ecf20Sopenharmony_ci 4128c2ecf20Sopenharmony_ci for (conn = system->driver_data; conn->dev_name; conn++) { 4138c2ecf20Sopenharmony_ci if (!strcmp(dev_name(&client->dev), conn->dev_name)) { 4148c2ecf20Sopenharmony_ci struct device *d; 4158c2ecf20Sopenharmony_ci 4168c2ecf20Sopenharmony_ci d = bus_find_device_by_name(&pci_bus_type, NULL, 4178c2ecf20Sopenharmony_ci conn->pci_name); 4188c2ecf20Sopenharmony_ci if (!d) 4198c2ecf20Sopenharmony_ci return -EPROBE_DEFER; 4208c2ecf20Sopenharmony_ci 4218c2ecf20Sopenharmony_ci put_device(d); 4228c2ecf20Sopenharmony_ci 4238c2ecf20Sopenharmony_ci *dev = d; 4248c2ecf20Sopenharmony_ci *port = conn->port_name; 4258c2ecf20Sopenharmony_ci 4268c2ecf20Sopenharmony_ci return 0; 4278c2ecf20Sopenharmony_ci } 4288c2ecf20Sopenharmony_ci } 4298c2ecf20Sopenharmony_ci 4308c2ecf20Sopenharmony_ci return 0; 4318c2ecf20Sopenharmony_ci} 4328c2ecf20Sopenharmony_ci 4338c2ecf20Sopenharmony_ci#else 4348c2ecf20Sopenharmony_ci 4358c2ecf20Sopenharmony_cistatic int ch7322_get_port(struct i2c_client *client, 4368c2ecf20Sopenharmony_ci struct device **dev, 4378c2ecf20Sopenharmony_ci const char **port) 4388c2ecf20Sopenharmony_ci{ 4398c2ecf20Sopenharmony_ci *dev = NULL; 4408c2ecf20Sopenharmony_ci *port = NULL; 4418c2ecf20Sopenharmony_ci 4428c2ecf20Sopenharmony_ci return 0; 4438c2ecf20Sopenharmony_ci} 4448c2ecf20Sopenharmony_ci 4458c2ecf20Sopenharmony_ci#endif 4468c2ecf20Sopenharmony_ci 4478c2ecf20Sopenharmony_cistatic int ch7322_probe(struct i2c_client *client) 4488c2ecf20Sopenharmony_ci{ 4498c2ecf20Sopenharmony_ci struct device *hdmi_dev; 4508c2ecf20Sopenharmony_ci const char *port_name; 4518c2ecf20Sopenharmony_ci struct ch7322 *ch7322; 4528c2ecf20Sopenharmony_ci struct cec_notifier *notifier = NULL; 4538c2ecf20Sopenharmony_ci u32 caps = CEC_CAP_DEFAULTS; 4548c2ecf20Sopenharmony_ci int ret; 4558c2ecf20Sopenharmony_ci unsigned int val; 4568c2ecf20Sopenharmony_ci 4578c2ecf20Sopenharmony_ci ret = ch7322_get_port(client, &hdmi_dev, &port_name); 4588c2ecf20Sopenharmony_ci if (ret) 4598c2ecf20Sopenharmony_ci return ret; 4608c2ecf20Sopenharmony_ci 4618c2ecf20Sopenharmony_ci if (hdmi_dev) 4628c2ecf20Sopenharmony_ci caps |= CEC_CAP_CONNECTOR_INFO; 4638c2ecf20Sopenharmony_ci 4648c2ecf20Sopenharmony_ci ch7322 = devm_kzalloc(&client->dev, sizeof(*ch7322), GFP_KERNEL); 4658c2ecf20Sopenharmony_ci if (!ch7322) 4668c2ecf20Sopenharmony_ci return -ENOMEM; 4678c2ecf20Sopenharmony_ci 4688c2ecf20Sopenharmony_ci ch7322->regmap = devm_regmap_init_i2c(client, &ch7322_regmap); 4698c2ecf20Sopenharmony_ci if (IS_ERR(ch7322->regmap)) 4708c2ecf20Sopenharmony_ci return PTR_ERR(ch7322->regmap); 4718c2ecf20Sopenharmony_ci 4728c2ecf20Sopenharmony_ci ret = regmap_read(ch7322->regmap, CH7322_DID, &val); 4738c2ecf20Sopenharmony_ci if (ret) 4748c2ecf20Sopenharmony_ci return ret; 4758c2ecf20Sopenharmony_ci 4768c2ecf20Sopenharmony_ci if (val != CH7322_DID_CH7322) 4778c2ecf20Sopenharmony_ci return -EOPNOTSUPP; 4788c2ecf20Sopenharmony_ci 4798c2ecf20Sopenharmony_ci mutex_init(&ch7322->mutex); 4808c2ecf20Sopenharmony_ci ch7322->i2c = client; 4818c2ecf20Sopenharmony_ci ch7322->tx_flags = 0; 4828c2ecf20Sopenharmony_ci 4838c2ecf20Sopenharmony_ci i2c_set_clientdata(client, ch7322); 4848c2ecf20Sopenharmony_ci 4858c2ecf20Sopenharmony_ci /* Disable auto mode */ 4868c2ecf20Sopenharmony_ci ret = regmap_write(ch7322->regmap, CH7322_MODE, CH7322_MODE_SW); 4878c2ecf20Sopenharmony_ci if (ret) 4888c2ecf20Sopenharmony_ci goto err_mutex; 4898c2ecf20Sopenharmony_ci 4908c2ecf20Sopenharmony_ci /* Enable logical address register */ 4918c2ecf20Sopenharmony_ci ret = regmap_update_bits(ch7322->regmap, CH7322_CTL, 4928c2ecf20Sopenharmony_ci CH7322_CTL_SPADL, CH7322_CTL_SPADL); 4938c2ecf20Sopenharmony_ci if (ret) 4948c2ecf20Sopenharmony_ci goto err_mutex; 4958c2ecf20Sopenharmony_ci 4968c2ecf20Sopenharmony_ci ch7322->cec = cec_allocate_adapter(&ch7322_cec_adap_ops, ch7322, 4978c2ecf20Sopenharmony_ci dev_name(&client->dev), 4988c2ecf20Sopenharmony_ci caps, 1); 4998c2ecf20Sopenharmony_ci 5008c2ecf20Sopenharmony_ci if (IS_ERR(ch7322->cec)) { 5018c2ecf20Sopenharmony_ci ret = PTR_ERR(ch7322->cec); 5028c2ecf20Sopenharmony_ci goto err_mutex; 5038c2ecf20Sopenharmony_ci } 5048c2ecf20Sopenharmony_ci 5058c2ecf20Sopenharmony_ci ch7322->cec->adap_controls_phys_addr = true; 5068c2ecf20Sopenharmony_ci 5078c2ecf20Sopenharmony_ci if (hdmi_dev) { 5088c2ecf20Sopenharmony_ci notifier = cec_notifier_cec_adap_register(hdmi_dev, 5098c2ecf20Sopenharmony_ci port_name, 5108c2ecf20Sopenharmony_ci ch7322->cec); 5118c2ecf20Sopenharmony_ci if (!notifier) { 5128c2ecf20Sopenharmony_ci ret = -ENOMEM; 5138c2ecf20Sopenharmony_ci goto err_cec; 5148c2ecf20Sopenharmony_ci } 5158c2ecf20Sopenharmony_ci } 5168c2ecf20Sopenharmony_ci 5178c2ecf20Sopenharmony_ci /* Configure, mask, and clear interrupt */ 5188c2ecf20Sopenharmony_ci ret = regmap_write(ch7322->regmap, CH7322_CFG1, 0); 5198c2ecf20Sopenharmony_ci if (ret) 5208c2ecf20Sopenharmony_ci goto err_notifier; 5218c2ecf20Sopenharmony_ci ret = regmap_write(ch7322->regmap, CH7322_INTCTL, CH7322_INTCTL_INTPB); 5228c2ecf20Sopenharmony_ci if (ret) 5238c2ecf20Sopenharmony_ci goto err_notifier; 5248c2ecf20Sopenharmony_ci ret = regmap_write(ch7322->regmap, CH7322_INTDATA, 0xff); 5258c2ecf20Sopenharmony_ci if (ret) 5268c2ecf20Sopenharmony_ci goto err_notifier; 5278c2ecf20Sopenharmony_ci 5288c2ecf20Sopenharmony_ci /* If HPD is up read physical address */ 5298c2ecf20Sopenharmony_ci ret = regmap_read(ch7322->regmap, CH7322_ADDLR, &val); 5308c2ecf20Sopenharmony_ci if (ret) 5318c2ecf20Sopenharmony_ci goto err_notifier; 5328c2ecf20Sopenharmony_ci if (val & CH7322_ADDLR_HPD) 5338c2ecf20Sopenharmony_ci ch7322_phys_addr(ch7322); 5348c2ecf20Sopenharmony_ci 5358c2ecf20Sopenharmony_ci ret = devm_request_threaded_irq(&client->dev, client->irq, NULL, 5368c2ecf20Sopenharmony_ci ch7322_irq, 5378c2ecf20Sopenharmony_ci IRQF_ONESHOT | IRQF_TRIGGER_RISING, 5388c2ecf20Sopenharmony_ci client->name, ch7322); 5398c2ecf20Sopenharmony_ci if (ret) 5408c2ecf20Sopenharmony_ci goto err_notifier; 5418c2ecf20Sopenharmony_ci 5428c2ecf20Sopenharmony_ci /* Unmask interrupt */ 5438c2ecf20Sopenharmony_ci mutex_lock(&ch7322->mutex); 5448c2ecf20Sopenharmony_ci ret = regmap_write(ch7322->regmap, CH7322_INTCTL, 0xff); 5458c2ecf20Sopenharmony_ci mutex_unlock(&ch7322->mutex); 5468c2ecf20Sopenharmony_ci 5478c2ecf20Sopenharmony_ci if (ret) 5488c2ecf20Sopenharmony_ci goto err_notifier; 5498c2ecf20Sopenharmony_ci 5508c2ecf20Sopenharmony_ci ret = cec_register_adapter(ch7322->cec, &client->dev); 5518c2ecf20Sopenharmony_ci if (ret) 5528c2ecf20Sopenharmony_ci goto err_notifier; 5538c2ecf20Sopenharmony_ci 5548c2ecf20Sopenharmony_ci dev_info(&client->dev, "device registered\n"); 5558c2ecf20Sopenharmony_ci 5568c2ecf20Sopenharmony_ci return 0; 5578c2ecf20Sopenharmony_ci 5588c2ecf20Sopenharmony_cierr_notifier: 5598c2ecf20Sopenharmony_ci if (notifier) 5608c2ecf20Sopenharmony_ci cec_notifier_cec_adap_unregister(notifier, ch7322->cec); 5618c2ecf20Sopenharmony_cierr_cec: 5628c2ecf20Sopenharmony_ci cec_delete_adapter(ch7322->cec); 5638c2ecf20Sopenharmony_cierr_mutex: 5648c2ecf20Sopenharmony_ci mutex_destroy(&ch7322->mutex); 5658c2ecf20Sopenharmony_ci return ret; 5668c2ecf20Sopenharmony_ci} 5678c2ecf20Sopenharmony_ci 5688c2ecf20Sopenharmony_cistatic int ch7322_remove(struct i2c_client *client) 5698c2ecf20Sopenharmony_ci{ 5708c2ecf20Sopenharmony_ci struct ch7322 *ch7322 = i2c_get_clientdata(client); 5718c2ecf20Sopenharmony_ci 5728c2ecf20Sopenharmony_ci /* Mask interrupt */ 5738c2ecf20Sopenharmony_ci mutex_lock(&ch7322->mutex); 5748c2ecf20Sopenharmony_ci regmap_write(ch7322->regmap, CH7322_INTCTL, CH7322_INTCTL_INTPB); 5758c2ecf20Sopenharmony_ci mutex_unlock(&ch7322->mutex); 5768c2ecf20Sopenharmony_ci 5778c2ecf20Sopenharmony_ci cec_unregister_adapter(ch7322->cec); 5788c2ecf20Sopenharmony_ci mutex_destroy(&ch7322->mutex); 5798c2ecf20Sopenharmony_ci 5808c2ecf20Sopenharmony_ci dev_info(&client->dev, "device unregistered\n"); 5818c2ecf20Sopenharmony_ci 5828c2ecf20Sopenharmony_ci return 0; 5838c2ecf20Sopenharmony_ci} 5848c2ecf20Sopenharmony_ci 5858c2ecf20Sopenharmony_cistatic const struct of_device_id ch7322_of_match[] = { 5868c2ecf20Sopenharmony_ci { .compatible = "chrontel,ch7322", }, 5878c2ecf20Sopenharmony_ci {}, 5888c2ecf20Sopenharmony_ci}; 5898c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, ch7322_of_match); 5908c2ecf20Sopenharmony_ci 5918c2ecf20Sopenharmony_cistatic struct i2c_driver ch7322_i2c_driver = { 5928c2ecf20Sopenharmony_ci .driver = { 5938c2ecf20Sopenharmony_ci .name = "ch7322", 5948c2ecf20Sopenharmony_ci .of_match_table = of_match_ptr(ch7322_of_match), 5958c2ecf20Sopenharmony_ci }, 5968c2ecf20Sopenharmony_ci .probe_new = ch7322_probe, 5978c2ecf20Sopenharmony_ci .remove = ch7322_remove, 5988c2ecf20Sopenharmony_ci}; 5998c2ecf20Sopenharmony_ci 6008c2ecf20Sopenharmony_cimodule_i2c_driver(ch7322_i2c_driver); 6018c2ecf20Sopenharmony_ci 6028c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Chrontel CH7322 CEC Controller Driver"); 6038c2ecf20Sopenharmony_ciMODULE_AUTHOR("Jeff Chase <jnchase@google.com>"); 6048c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 605