1494N/A/*
1494N/A * Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved.
1494N/A */
1494N/A/*
1494N/A * Copyright (c) 2012, 2013 Intel Corporation. All rights reserved.
1494N/A */
1494N/A/**************************************************************************
1494N/A
1494N/ACopyright © 2006 Dave Airlie
1494N/A
1494N/AAll Rights Reserved.
1494N/A
1494N/APermission is hereby granted, free of charge, to any person obtaining a
1494N/Acopy of this software and associated documentation files (the
1494N/A"Software"), to deal in the Software without restriction, including
1494N/Awithout limitation the rights to use, copy, modify, merge, publish,
1494N/Adistribute, sub license, and/or sell copies of the Software, and to
1494N/Apermit persons to whom the Software is furnished to do so, subject to
1494N/Athe following conditions:
1494N/A
1494N/AThe above copyright notice and this permission notice (including the
1494N/Anext paragraph) shall be included in all copies or substantial portions
1494N/Aof the Software.
1494N/A
1494N/ATHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
1494N/AOR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
1494N/AMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
1494N/AIN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1494N/AANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
1494N/ATORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
1494N/ASOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1494N/A
1494N/A**************************************************************************/
1494N/A
1494N/A#include "dvo.h"
1494N/A
1494N/A#define CH7xxx_REG_VID 0x4a
1494N/A#define CH7xxx_REG_DID 0x4b
1494N/A
1494N/A#define CH7011_VID 0x83 /* 7010 as well */
1494N/A#define CH7010B_VID 0x05
1494N/A#define CH7009A_VID 0x84
1494N/A#define CH7009B_VID 0x85
1494N/A#define CH7301_VID 0x95
1494N/A
1494N/A#define CH7xxx_VID 0x84
1494N/A#define CH7xxx_DID 0x17
1494N/A#define CH7010_DID 0x16
1494N/A
1494N/A#define CH7xxx_NUM_REGS 0x4c
1494N/A
1494N/A#define CH7xxx_CM 0x1c
1494N/A#define CH7xxx_CM_XCM (1<<0)
1494N/A#define CH7xxx_CM_MCP (1<<2)
1494N/A#define CH7xxx_INPUT_CLOCK 0x1d
1494N/A#define CH7xxx_GPIO 0x1e
1494N/A#define CH7xxx_GPIO_HPIR (1<<3)
1494N/A#define CH7xxx_IDF 0x1f
1494N/A
1494N/A#define CH7xxx_IDF_HSP (1<<3)
1494N/A#define CH7xxx_IDF_VSP (1<<4)
1494N/A
1494N/A#define CH7xxx_CONNECTION_DETECT 0x20
1494N/A#define CH7xxx_CDET_DVI (1<<5)
1494N/A
1494N/A#define CH7301_DAC_CNTL 0x21
1494N/A#define CH7301_HOTPLUG 0x23
1494N/A#define CH7xxx_TCTL 0x31
1494N/A#define CH7xxx_TVCO 0x32
1494N/A#define CH7xxx_TPCP 0x33
1494N/A#define CH7xxx_TPD 0x34
1494N/A#define CH7xxx_TPVT 0x35
1494N/A#define CH7xxx_TLPF 0x36
1494N/A#define CH7xxx_TCT 0x37
1494N/A#define CH7301_TEST_PATTERN 0x48
1494N/A
1494N/A#define CH7xxx_PM 0x49
1494N/A#define CH7xxx_PM_FPD (1<<0)
1494N/A#define CH7301_PM_DACPD0 (1<<1)
1494N/A#define CH7301_PM_DACPD1 (1<<2)
1494N/A#define CH7301_PM_DACPD2 (1<<3)
1494N/A#define CH7xxx_PM_DVIL (1<<6)
1494N/A#define CH7xxx_PM_DVIP (1<<7)
1494N/A
1494N/A#define CH7301_SYNC_POLARITY 0x56
1494N/A#define CH7301_SYNC_RGB_YUV (1<<0)
1494N/A#define CH7301_SYNC_POL_DVI (1<<5)
1494N/A
1494N/A/** @file
1494N/A * driver for the Chrontel 7xxx DVI chip over DVO.
1494N/A */
1494N/A
1494N/Astatic struct ch7xxx_id_struct {
1494N/A uint8_t vid;
1494N/A char *name;
1494N/A} ch7xxx_ids[] = {
1494N/A { CH7011_VID, "CH7011" },
1494N/A { CH7010B_VID, "CH7010B" },
1494N/A { CH7009A_VID, "CH7009A" },
1494N/A { CH7009B_VID, "CH7009B" },
1494N/A { CH7301_VID, "CH7301" },
1494N/A};
1494N/A
1494N/Astatic struct ch7xxx_did_struct {
1494N/A uint8_t did;
1494N/A char *name;
1494N/A} ch7xxx_dids[] = {
1494N/A { CH7xxx_DID, "CH7XXX" },
1494N/A { CH7010_DID, "CH7010B" },
1494N/A};
1494N/A
1494N/Astruct ch7xxx_priv {
1494N/A bool quiet;
1494N/A};
1494N/A
1494N/Astatic char *ch7xxx_get_id(uint8_t vid)
1494N/A{
1494N/A int i;
1494N/A
1494N/A for (i = 0; i < ARRAY_SIZE(ch7xxx_ids); i++) {
1494N/A if (ch7xxx_ids[i].vid == vid)
1494N/A return ch7xxx_ids[i].name;
1494N/A }
1494N/A
1494N/A return NULL;
1494N/A}
1494N/A
1494N/Astatic char *ch7xxx_get_did(uint8_t did)
1494N/A{
1494N/A int i;
1494N/A
1494N/A for (i = 0; i < ARRAY_SIZE(ch7xxx_dids); i++) {
1494N/A if (ch7xxx_dids[i].did == did)
1494N/A return ch7xxx_dids[i].name;
1494N/A }
1494N/A
1494N/A return NULL;
1494N/A}
1494N/A
1494N/A/** Reads an 8 bit register */
1494N/Astatic bool ch7xxx_readb(struct intel_dvo_device *dvo, int addr, uint8_t *ch)
1494N/A{
1494N/A struct ch7xxx_priv *ch7xxx= dvo->dev_priv;
1494N/A struct i2c_adapter *adapter = dvo->i2c_bus;
1494N/A u8 out_buf[2];
1494N/A u8 in_buf[2];
1494N/A
1494N/A struct i2c_msg msgs[] = {
1494N/A {
1494N/A .addr = dvo->slave_addr,
1494N/A .flags = 0,
1494N/A .len = 1,
1494N/A .buf = out_buf,
1494N/A },
1494N/A {
1494N/A .addr = dvo->slave_addr,
1494N/A .flags = I2C_M_RD,
1494N/A .len = 1,
1494N/A .buf = in_buf,
1494N/A }
1494N/A };
1494N/A
1494N/A out_buf[0] = (u8) addr;
1494N/A out_buf[1] = 0;
1494N/A
1494N/A if (i2c_transfer(adapter, msgs, 2) == 2) {
1494N/A *ch = in_buf[0];
1494N/A return true;
1494N/A };
1494N/A
1494N/A if (!ch7xxx->quiet) {
1494N/A DRM_DEBUG_KMS("Unable to read register 0x%02x from %s:%02x.\n",
1494N/A addr, adapter->name, dvo->slave_addr);
1494N/A }
1494N/A return false;
1494N/A}
1494N/A
1494N/A/** Writes an 8 bit register */
1494N/Astatic bool ch7xxx_writeb(struct intel_dvo_device *dvo, int addr, uint8_t ch)
1494N/A{
1494N/A struct ch7xxx_priv *ch7xxx = dvo->dev_priv;
1494N/A struct i2c_adapter *adapter = dvo->i2c_bus;
1494N/A uint8_t out_buf[2];
1494N/A struct i2c_msg msg = {
1494N/A .addr = dvo->slave_addr,
1494N/A .flags = 0,
1494N/A .len = 2,
1494N/A .buf = out_buf,
1494N/A };
1494N/A
1494N/A out_buf[0] = (uint8_t) addr;
1494N/A out_buf[1] = ch;
1494N/A
1494N/A if (i2c_transfer(adapter, &msg, 1) == 1)
1494N/A return true;
1494N/A
1494N/A if (!ch7xxx->quiet) {
1494N/A DRM_DEBUG_KMS("Unable to write register 0x%02x to %s:%d.\n",
1494N/A addr, adapter->name, dvo->slave_addr);
1494N/A }
1494N/A
1494N/A return false;
1494N/A}
1494N/A
1494N/Astatic bool ch7xxx_init(struct intel_dvo_device *dvo,
1494N/A struct i2c_adapter *adapter)
1494N/A{
1494N/A /* this will detect the CH7xxx chip on the specified i2c bus */
1494N/A struct ch7xxx_priv *ch7xxx;
1494N/A uint8_t vendor, device;
1494N/A char *name, *devid;
1494N/A
1494N/A ch7xxx = kzalloc(sizeof(struct ch7xxx_priv), GFP_KERNEL);
1494N/A if (ch7xxx == NULL)
1494N/A return false;
1494N/A
1494N/A dvo->i2c_bus = adapter;
1494N/A dvo->dev_priv = ch7xxx;
1494N/A ch7xxx->quiet = true;
1494N/A
1494N/A if (!ch7xxx_readb(dvo, CH7xxx_REG_VID, &vendor))
1494N/A goto out;
1494N/A
1494N/A name = ch7xxx_get_id(vendor);
1494N/A if (!name) {
1494N/A DRM_DEBUG_KMS("ch7xxx not detected; got 0x%02x from %s "
1494N/A "slave %d.\n",
1494N/A vendor, adapter->name, dvo->slave_addr);
1494N/A goto out;
1494N/A }
1494N/A
1494N/A
1494N/A if (!ch7xxx_readb(dvo, CH7xxx_REG_DID, &device))
1494N/A goto out;
1494N/A
1494N/A devid = ch7xxx_get_did(device);
1494N/A if (!devid) {
1494N/A DRM_DEBUG_KMS("ch7xxx not detected; got 0x%02x from %s "
1494N/A "slave %d.\n",
1494N/A vendor, adapter->name, dvo->slave_addr);
1494N/A goto out;
1494N/A }
1494N/A
1494N/A ch7xxx->quiet = false;
1494N/A DRM_DEBUG_KMS("Detected %s chipset, vendor/device ID 0x%02x/0x%02x\n",
1494N/A name, vendor, device);
1494N/A return true;
1494N/Aout:
1494N/A kfree(ch7xxx, sizeof (struct ch7xxx_priv));
1494N/A return false;
1494N/A}
1494N/A
1494N/Astatic enum drm_connector_status ch7xxx_detect(struct intel_dvo_device *dvo)
1494N/A{
1494N/A uint8_t cdet, orig_pm, pm;
1494N/A
1494N/A (void) ch7xxx_readb(dvo, CH7xxx_PM, &orig_pm);
1494N/A
1494N/A pm = orig_pm;
1494N/A pm &= ~CH7xxx_PM_FPD;
1494N/A pm |= CH7xxx_PM_DVIL | CH7xxx_PM_DVIP;
1494N/A
1494N/A (void) ch7xxx_writeb(dvo, CH7xxx_PM, pm);
1494N/A
1494N/A (void) ch7xxx_readb(dvo, CH7xxx_CONNECTION_DETECT, &cdet);
1494N/A
1494N/A (void) ch7xxx_writeb(dvo, CH7xxx_PM, orig_pm);
1494N/A
1494N/A if (cdet & CH7xxx_CDET_DVI)
1494N/A return connector_status_connected;
1494N/A return connector_status_disconnected;
1494N/A}
1494N/A
1494N/A/* LINTED */
1494N/Astatic int ch7xxx_mode_valid(struct intel_dvo_device *dvo,
1494N/A struct drm_display_mode *mode)
1494N/A{
1494N/A if (mode->clock > 165000)
1494N/A return MODE_CLOCK_HIGH;
1494N/A
1494N/A return MODE_OK;
1494N/A}
1494N/A
1494N/Astatic void ch7xxx_mode_set(struct intel_dvo_device *dvo,
1494N/A struct drm_display_mode *mode,
1494N/A /* LINTED */
1494N/A struct drm_display_mode *adjusted_mode)
1494N/A{
1494N/A uint8_t tvco, tpcp, tpd, tlpf, idf;
1494N/A
1494N/A if (mode->clock <= 65000) {
1494N/A tvco = 0x23;
1494N/A tpcp = 0x08;
1494N/A tpd = 0x16;
1494N/A tlpf = 0x60;
1494N/A } else {
1494N/A tvco = 0x2d;
1494N/A tpcp = 0x06;
1494N/A tpd = 0x26;
1494N/A tlpf = 0xa0;
1494N/A }
1494N/A
1494N/A (void) ch7xxx_writeb(dvo, CH7xxx_TCTL, 0x00);
1494N/A (void) ch7xxx_writeb(dvo, CH7xxx_TVCO, tvco);
1494N/A (void) ch7xxx_writeb(dvo, CH7xxx_TPCP, tpcp);
1494N/A (void) ch7xxx_writeb(dvo, CH7xxx_TPD, tpd);
1494N/A (void) ch7xxx_writeb(dvo, CH7xxx_TPVT, 0x30);
1494N/A (void) ch7xxx_writeb(dvo, CH7xxx_TLPF, tlpf);
1494N/A (void) ch7xxx_writeb(dvo, CH7xxx_TCT, 0x00);
1494N/A
1494N/A (void) ch7xxx_readb(dvo, CH7xxx_IDF, &idf);
1494N/A
1494N/A idf &= ~(CH7xxx_IDF_HSP | CH7xxx_IDF_VSP);
1494N/A if (mode->flags & DRM_MODE_FLAG_PHSYNC)
1494N/A idf |= CH7xxx_IDF_HSP;
1494N/A
1494N/A if (mode->flags & DRM_MODE_FLAG_PVSYNC)
1494N/A idf |= CH7xxx_IDF_HSP;
1494N/A
1494N/A (void) ch7xxx_writeb(dvo, CH7xxx_IDF, idf);
1494N/A}
1494N/A
1494N/A/* set the CH7xxx power state */
1494N/Astatic void ch7xxx_dpms(struct intel_dvo_device *dvo, bool enable)
1494N/A{
1494N/A if (enable)
1494N/A (void) ch7xxx_writeb(dvo, CH7xxx_PM, CH7xxx_PM_DVIL | CH7xxx_PM_DVIP);
1494N/A else
1494N/A (void) ch7xxx_writeb(dvo, CH7xxx_PM, CH7xxx_PM_FPD);
1494N/A}
1494N/A
1494N/Astatic bool ch7xxx_get_hw_state(struct intel_dvo_device *dvo)
1494N/A{
1494N/A u8 val;
1494N/A
1494N/A ch7xxx_readb(dvo, CH7xxx_PM, &val);
1494N/A
1494N/A if (val & (CH7xxx_PM_DVIL | CH7xxx_PM_DVIP))
1494N/A return true;
1494N/A else
1494N/A return false;
1494N/A}
1494N/A
1494N/Astatic void ch7xxx_dump_regs(struct intel_dvo_device *dvo)
1494N/A{
1494N/A int i;
1494N/A
1494N/A for (i = 0; i < CH7xxx_NUM_REGS; i++) {
1494N/A uint8_t val;
1494N/A if ((i % 8) == 0 )
1494N/A DRM_LOG_KMS("\n %02X: ", i);
1494N/A (void) ch7xxx_readb(dvo, i, &val);
1494N/A DRM_LOG_KMS("%02X ", val);
1494N/A}
1494N/A}
1494N/A
1494N/Astatic void ch7xxx_destroy(struct intel_dvo_device *dvo)
1494N/A{
1494N/A struct ch7xxx_priv *ch7xxx = dvo->dev_priv;
1494N/A
1494N/A if (ch7xxx) {
1494N/A kfree(ch7xxx, sizeof (struct ch7xxx_priv));
1494N/A dvo->dev_priv = NULL;
1494N/A }
1494N/A}
1494N/A
1494N/Astruct intel_dvo_dev_ops ch7xxx_ops = {
1494N/A .init = ch7xxx_init,
1494N/A .detect = ch7xxx_detect,
1494N/A .mode_valid = ch7xxx_mode_valid,
1494N/A .mode_set = ch7xxx_mode_set,
1494N/A .dpms = ch7xxx_dpms,
1494N/A .get_hw_state = ch7xxx_get_hw_state,
1494N/A .dump_regs = ch7xxx_dump_regs,
1494N/A .destroy = ch7xxx_destroy,
1494N/A};