Home | History | Annotate | Line # | Download | only in display
      1  1.1  riastrad /*	$NetBSD: dvo_tfp410.c,v 1.2 2021/12/18 23:45:29 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * Copyright  2007 Dave Mueller
      5  1.1  riastrad  *
      6  1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
      7  1.1  riastrad  * copy of this software and associated documentation files (the "Software"),
      8  1.1  riastrad  * to deal in the Software without restriction, including without limitation
      9  1.1  riastrad  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  1.1  riastrad  * and/or sell copies of the Software, and to permit persons to whom the
     11  1.1  riastrad  * Software is furnished to do so, subject to the following conditions:
     12  1.1  riastrad  *
     13  1.1  riastrad  * The above copyright notice and this permission notice (including the next
     14  1.1  riastrad  * paragraph) shall be included in all copies or substantial portions of the
     15  1.1  riastrad  * Software.
     16  1.1  riastrad  *
     17  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     18  1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     19  1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     20  1.1  riastrad  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     21  1.1  riastrad  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     22  1.1  riastrad  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
     23  1.1  riastrad  * IN THE SOFTWARE.
     24  1.1  riastrad  *
     25  1.1  riastrad  * Authors:
     26  1.1  riastrad  *    Dave Mueller <dave.mueller (at) gmx.ch>
     27  1.1  riastrad  *
     28  1.1  riastrad  */
     29  1.1  riastrad 
     30  1.1  riastrad #include <sys/cdefs.h>
     31  1.1  riastrad __KERNEL_RCSID(0, "$NetBSD: dvo_tfp410.c,v 1.2 2021/12/18 23:45:29 riastradh Exp $");
     32  1.1  riastrad 
     33  1.1  riastrad #include "intel_display_types.h"
     34  1.1  riastrad #include "intel_dvo_dev.h"
     35  1.1  riastrad 
     36  1.1  riastrad /* register definitions according to the TFP410 data sheet */
     37  1.1  riastrad #define TFP410_VID		0x014C
     38  1.1  riastrad #define TFP410_DID		0x0410
     39  1.1  riastrad 
     40  1.1  riastrad #define TFP410_VID_LO		0x00
     41  1.1  riastrad #define TFP410_VID_HI		0x01
     42  1.1  riastrad #define TFP410_DID_LO		0x02
     43  1.1  riastrad #define TFP410_DID_HI		0x03
     44  1.1  riastrad #define TFP410_REV		0x04
     45  1.1  riastrad 
     46  1.1  riastrad #define TFP410_CTL_1		0x08
     47  1.1  riastrad #define TFP410_CTL_1_TDIS	(1<<6)
     48  1.1  riastrad #define TFP410_CTL_1_VEN	(1<<5)
     49  1.1  riastrad #define TFP410_CTL_1_HEN	(1<<4)
     50  1.1  riastrad #define TFP410_CTL_1_DSEL	(1<<3)
     51  1.1  riastrad #define TFP410_CTL_1_BSEL	(1<<2)
     52  1.1  riastrad #define TFP410_CTL_1_EDGE	(1<<1)
     53  1.1  riastrad #define TFP410_CTL_1_PD		(1<<0)
     54  1.1  riastrad 
     55  1.1  riastrad #define TFP410_CTL_2		0x09
     56  1.1  riastrad #define TFP410_CTL_2_VLOW	(1<<7)
     57  1.1  riastrad #define TFP410_CTL_2_MSEL_MASK	(0x7<<4)
     58  1.1  riastrad #define TFP410_CTL_2_MSEL	(1<<4)
     59  1.1  riastrad #define TFP410_CTL_2_TSEL	(1<<3)
     60  1.1  riastrad #define TFP410_CTL_2_RSEN	(1<<2)
     61  1.1  riastrad #define TFP410_CTL_2_HTPLG	(1<<1)
     62  1.1  riastrad #define TFP410_CTL_2_MDI	(1<<0)
     63  1.1  riastrad 
     64  1.1  riastrad #define TFP410_CTL_3		0x0A
     65  1.1  riastrad #define TFP410_CTL_3_DK_MASK	(0x7<<5)
     66  1.1  riastrad #define TFP410_CTL_3_DK		(1<<5)
     67  1.1  riastrad #define TFP410_CTL_3_DKEN	(1<<4)
     68  1.1  riastrad #define TFP410_CTL_3_CTL_MASK	(0x7<<1)
     69  1.1  riastrad #define TFP410_CTL_3_CTL	(1<<1)
     70  1.1  riastrad 
     71  1.1  riastrad #define TFP410_USERCFG		0x0B
     72  1.1  riastrad 
     73  1.1  riastrad #define TFP410_DE_DLY		0x32
     74  1.1  riastrad 
     75  1.1  riastrad #define TFP410_DE_CTL		0x33
     76  1.1  riastrad #define TFP410_DE_CTL_DEGEN	(1<<6)
     77  1.1  riastrad #define TFP410_DE_CTL_VSPOL	(1<<5)
     78  1.1  riastrad #define TFP410_DE_CTL_HSPOL	(1<<4)
     79  1.1  riastrad #define TFP410_DE_CTL_DEDLY8	(1<<0)
     80  1.1  riastrad 
     81  1.1  riastrad #define TFP410_DE_TOP		0x34
     82  1.1  riastrad 
     83  1.1  riastrad #define TFP410_DE_CNT_LO	0x36
     84  1.1  riastrad #define TFP410_DE_CNT_HI	0x37
     85  1.1  riastrad 
     86  1.1  riastrad #define TFP410_DE_LIN_LO	0x38
     87  1.1  riastrad #define TFP410_DE_LIN_HI	0x39
     88  1.1  riastrad 
     89  1.1  riastrad #define TFP410_H_RES_LO		0x3A
     90  1.1  riastrad #define TFP410_H_RES_HI		0x3B
     91  1.1  riastrad 
     92  1.1  riastrad #define TFP410_V_RES_LO		0x3C
     93  1.1  riastrad #define TFP410_V_RES_HI		0x3D
     94  1.1  riastrad 
     95  1.1  riastrad struct tfp410_priv {
     96  1.1  riastrad 	bool quiet;
     97  1.1  riastrad };
     98  1.1  riastrad 
     99  1.1  riastrad static bool tfp410_readb(struct intel_dvo_device *dvo, int addr, u8 *ch)
    100  1.1  riastrad {
    101  1.1  riastrad 	struct tfp410_priv *tfp = dvo->dev_priv;
    102  1.1  riastrad 	struct i2c_adapter *adapter = dvo->i2c_bus;
    103  1.1  riastrad 	u8 out_buf[2];
    104  1.1  riastrad 	u8 in_buf[2];
    105  1.1  riastrad 
    106  1.1  riastrad 	struct i2c_msg msgs[] = {
    107  1.1  riastrad 		{
    108  1.1  riastrad 			.addr = dvo->slave_addr,
    109  1.1  riastrad 			.flags = 0,
    110  1.1  riastrad 			.len = 1,
    111  1.1  riastrad 			.buf = out_buf,
    112  1.1  riastrad 		},
    113  1.1  riastrad 		{
    114  1.1  riastrad 			.addr = dvo->slave_addr,
    115  1.1  riastrad 			.flags = I2C_M_RD,
    116  1.1  riastrad 			.len = 1,
    117  1.1  riastrad 			.buf = in_buf,
    118  1.1  riastrad 		}
    119  1.1  riastrad 	};
    120  1.1  riastrad 
    121  1.1  riastrad 	out_buf[0] = addr;
    122  1.1  riastrad 	out_buf[1] = 0;
    123  1.1  riastrad 
    124  1.1  riastrad 	if (i2c_transfer(adapter, msgs, 2) == 2) {
    125  1.1  riastrad 		*ch = in_buf[0];
    126  1.1  riastrad 		return true;
    127  1.1  riastrad 	}
    128  1.1  riastrad 
    129  1.1  riastrad 	if (!tfp->quiet) {
    130  1.1  riastrad 		DRM_DEBUG_KMS("Unable to read register 0x%02x from %s:%02x.\n",
    131  1.1  riastrad 			  addr, adapter->name, dvo->slave_addr);
    132  1.1  riastrad 	}
    133  1.1  riastrad 	return false;
    134  1.1  riastrad }
    135  1.1  riastrad 
    136  1.1  riastrad static bool tfp410_writeb(struct intel_dvo_device *dvo, int addr, u8 ch)
    137  1.1  riastrad {
    138  1.1  riastrad 	struct tfp410_priv *tfp = dvo->dev_priv;
    139  1.1  riastrad 	struct i2c_adapter *adapter = dvo->i2c_bus;
    140  1.1  riastrad 	u8 out_buf[2];
    141  1.1  riastrad 	struct i2c_msg msg = {
    142  1.1  riastrad 		.addr = dvo->slave_addr,
    143  1.1  riastrad 		.flags = 0,
    144  1.1  riastrad 		.len = 2,
    145  1.1  riastrad 		.buf = out_buf,
    146  1.1  riastrad 	};
    147  1.1  riastrad 
    148  1.1  riastrad 	out_buf[0] = addr;
    149  1.1  riastrad 	out_buf[1] = ch;
    150  1.1  riastrad 
    151  1.1  riastrad 	if (i2c_transfer(adapter, &msg, 1) == 1)
    152  1.1  riastrad 		return true;
    153  1.1  riastrad 
    154  1.1  riastrad 	if (!tfp->quiet) {
    155  1.1  riastrad 		DRM_DEBUG_KMS("Unable to write register 0x%02x to %s:%d.\n",
    156  1.1  riastrad 			  addr, adapter->name, dvo->slave_addr);
    157  1.1  riastrad 	}
    158  1.1  riastrad 
    159  1.1  riastrad 	return false;
    160  1.1  riastrad }
    161  1.1  riastrad 
    162  1.1  riastrad static int tfp410_getid(struct intel_dvo_device *dvo, int addr)
    163  1.1  riastrad {
    164  1.1  riastrad 	u8 ch1, ch2;
    165  1.1  riastrad 
    166  1.1  riastrad 	if (tfp410_readb(dvo, addr+0, &ch1) &&
    167  1.1  riastrad 	    tfp410_readb(dvo, addr+1, &ch2))
    168  1.1  riastrad 		return ((ch2 << 8) & 0xFF00) | (ch1 & 0x00FF);
    169  1.1  riastrad 
    170  1.1  riastrad 	return -1;
    171  1.1  riastrad }
    172  1.1  riastrad 
    173  1.1  riastrad /* Ti TFP410 driver for chip on i2c bus */
    174  1.1  riastrad static bool tfp410_init(struct intel_dvo_device *dvo,
    175  1.1  riastrad 			struct i2c_adapter *adapter)
    176  1.1  riastrad {
    177  1.1  riastrad 	/* this will detect the tfp410 chip on the specified i2c bus */
    178  1.1  riastrad 	struct tfp410_priv *tfp;
    179  1.1  riastrad 	int id;
    180  1.1  riastrad 
    181  1.1  riastrad 	tfp = kzalloc(sizeof(struct tfp410_priv), GFP_KERNEL);
    182  1.1  riastrad 	if (tfp == NULL)
    183  1.1  riastrad 		return false;
    184  1.1  riastrad 
    185  1.1  riastrad 	dvo->i2c_bus = adapter;
    186  1.1  riastrad 	dvo->dev_priv = tfp;
    187  1.1  riastrad 	tfp->quiet = true;
    188  1.1  riastrad 
    189  1.1  riastrad 	if ((id = tfp410_getid(dvo, TFP410_VID_LO)) != TFP410_VID) {
    190  1.1  riastrad 		DRM_DEBUG_KMS("tfp410 not detected got VID %X: from %s "
    191  1.1  riastrad 				"Slave %d.\n",
    192  1.1  riastrad 			  id, adapter->name, dvo->slave_addr);
    193  1.1  riastrad 		goto out;
    194  1.1  riastrad 	}
    195  1.1  riastrad 
    196  1.1  riastrad 	if ((id = tfp410_getid(dvo, TFP410_DID_LO)) != TFP410_DID) {
    197  1.1  riastrad 		DRM_DEBUG_KMS("tfp410 not detected got DID %X: from %s "
    198  1.1  riastrad 				"Slave %d.\n",
    199  1.1  riastrad 			  id, adapter->name, dvo->slave_addr);
    200  1.1  riastrad 		goto out;
    201  1.1  riastrad 	}
    202  1.1  riastrad 	tfp->quiet = false;
    203  1.1  riastrad 	return true;
    204  1.1  riastrad out:
    205  1.1  riastrad 	kfree(tfp);
    206  1.1  riastrad 	return false;
    207  1.1  riastrad }
    208  1.1  riastrad 
    209  1.1  riastrad static enum drm_connector_status tfp410_detect(struct intel_dvo_device *dvo)
    210  1.1  riastrad {
    211  1.1  riastrad 	enum drm_connector_status ret = connector_status_disconnected;
    212  1.1  riastrad 	u8 ctl2;
    213  1.1  riastrad 
    214  1.1  riastrad 	if (tfp410_readb(dvo, TFP410_CTL_2, &ctl2)) {
    215  1.1  riastrad 		if (ctl2 & TFP410_CTL_2_RSEN)
    216  1.1  riastrad 			ret = connector_status_connected;
    217  1.1  riastrad 		else
    218  1.1  riastrad 			ret = connector_status_disconnected;
    219  1.1  riastrad 	}
    220  1.1  riastrad 
    221  1.1  riastrad 	return ret;
    222  1.1  riastrad }
    223  1.1  riastrad 
    224  1.1  riastrad static enum drm_mode_status tfp410_mode_valid(struct intel_dvo_device *dvo,
    225  1.1  riastrad 					      struct drm_display_mode *mode)
    226  1.1  riastrad {
    227  1.1  riastrad 	return MODE_OK;
    228  1.1  riastrad }
    229  1.1  riastrad 
    230  1.1  riastrad static void tfp410_mode_set(struct intel_dvo_device *dvo,
    231  1.1  riastrad 			    const struct drm_display_mode *mode,
    232  1.1  riastrad 			    const struct drm_display_mode *adjusted_mode)
    233  1.1  riastrad {
    234  1.1  riastrad 	/* As long as the basics are set up, since we don't have clock dependencies
    235  1.1  riastrad 	* in the mode setup, we can just leave the registers alone and everything
    236  1.1  riastrad 	* will work fine.
    237  1.1  riastrad 	*/
    238  1.1  riastrad 	/* don't do much */
    239  1.1  riastrad 	return;
    240  1.1  riastrad }
    241  1.1  riastrad 
    242  1.1  riastrad /* set the tfp410 power state */
    243  1.1  riastrad static void tfp410_dpms(struct intel_dvo_device *dvo, bool enable)
    244  1.1  riastrad {
    245  1.1  riastrad 	u8 ctl1;
    246  1.1  riastrad 
    247  1.1  riastrad 	if (!tfp410_readb(dvo, TFP410_CTL_1, &ctl1))
    248  1.1  riastrad 		return;
    249  1.1  riastrad 
    250  1.1  riastrad 	if (enable)
    251  1.1  riastrad 		ctl1 |= TFP410_CTL_1_PD;
    252  1.1  riastrad 	else
    253  1.1  riastrad 		ctl1 &= ~TFP410_CTL_1_PD;
    254  1.1  riastrad 
    255  1.1  riastrad 	tfp410_writeb(dvo, TFP410_CTL_1, ctl1);
    256  1.1  riastrad }
    257  1.1  riastrad 
    258  1.1  riastrad static bool tfp410_get_hw_state(struct intel_dvo_device *dvo)
    259  1.1  riastrad {
    260  1.1  riastrad 	u8 ctl1;
    261  1.1  riastrad 
    262  1.1  riastrad 	if (!tfp410_readb(dvo, TFP410_CTL_1, &ctl1))
    263  1.1  riastrad 		return false;
    264  1.1  riastrad 
    265  1.1  riastrad 	if (ctl1 & TFP410_CTL_1_PD)
    266  1.1  riastrad 		return true;
    267  1.1  riastrad 	else
    268  1.1  riastrad 		return false;
    269  1.1  riastrad }
    270  1.1  riastrad 
    271  1.1  riastrad static void tfp410_dump_regs(struct intel_dvo_device *dvo)
    272  1.1  riastrad {
    273  1.1  riastrad 	u8 val, val2;
    274  1.1  riastrad 
    275  1.1  riastrad 	tfp410_readb(dvo, TFP410_REV, &val);
    276  1.1  riastrad 	DRM_DEBUG_KMS("TFP410_REV: 0x%02X\n", val);
    277  1.1  riastrad 	tfp410_readb(dvo, TFP410_CTL_1, &val);
    278  1.1  riastrad 	DRM_DEBUG_KMS("TFP410_CTL1: 0x%02X\n", val);
    279  1.1  riastrad 	tfp410_readb(dvo, TFP410_CTL_2, &val);
    280  1.1  riastrad 	DRM_DEBUG_KMS("TFP410_CTL2: 0x%02X\n", val);
    281  1.1  riastrad 	tfp410_readb(dvo, TFP410_CTL_3, &val);
    282  1.1  riastrad 	DRM_DEBUG_KMS("TFP410_CTL3: 0x%02X\n", val);
    283  1.1  riastrad 	tfp410_readb(dvo, TFP410_USERCFG, &val);
    284  1.1  riastrad 	DRM_DEBUG_KMS("TFP410_USERCFG: 0x%02X\n", val);
    285  1.1  riastrad 	tfp410_readb(dvo, TFP410_DE_DLY, &val);
    286  1.1  riastrad 	DRM_DEBUG_KMS("TFP410_DE_DLY: 0x%02X\n", val);
    287  1.1  riastrad 	tfp410_readb(dvo, TFP410_DE_CTL, &val);
    288  1.1  riastrad 	DRM_DEBUG_KMS("TFP410_DE_CTL: 0x%02X\n", val);
    289  1.1  riastrad 	tfp410_readb(dvo, TFP410_DE_TOP, &val);
    290  1.1  riastrad 	DRM_DEBUG_KMS("TFP410_DE_TOP: 0x%02X\n", val);
    291  1.1  riastrad 	tfp410_readb(dvo, TFP410_DE_CNT_LO, &val);
    292  1.1  riastrad 	tfp410_readb(dvo, TFP410_DE_CNT_HI, &val2);
    293  1.1  riastrad 	DRM_DEBUG_KMS("TFP410_DE_CNT: 0x%02X%02X\n", val2, val);
    294  1.1  riastrad 	tfp410_readb(dvo, TFP410_DE_LIN_LO, &val);
    295  1.1  riastrad 	tfp410_readb(dvo, TFP410_DE_LIN_HI, &val2);
    296  1.1  riastrad 	DRM_DEBUG_KMS("TFP410_DE_LIN: 0x%02X%02X\n", val2, val);
    297  1.1  riastrad 	tfp410_readb(dvo, TFP410_H_RES_LO, &val);
    298  1.1  riastrad 	tfp410_readb(dvo, TFP410_H_RES_HI, &val2);
    299  1.1  riastrad 	DRM_DEBUG_KMS("TFP410_H_RES: 0x%02X%02X\n", val2, val);
    300  1.1  riastrad 	tfp410_readb(dvo, TFP410_V_RES_LO, &val);
    301  1.1  riastrad 	tfp410_readb(dvo, TFP410_V_RES_HI, &val2);
    302  1.1  riastrad 	DRM_DEBUG_KMS("TFP410_V_RES: 0x%02X%02X\n", val2, val);
    303  1.1  riastrad }
    304  1.1  riastrad 
    305  1.1  riastrad static void tfp410_destroy(struct intel_dvo_device *dvo)
    306  1.1  riastrad {
    307  1.1  riastrad 	struct tfp410_priv *tfp = dvo->dev_priv;
    308  1.1  riastrad 
    309  1.1  riastrad 	if (tfp) {
    310  1.1  riastrad 		kfree(tfp);
    311  1.1  riastrad 		dvo->dev_priv = NULL;
    312  1.1  riastrad 	}
    313  1.1  riastrad }
    314  1.1  riastrad 
    315  1.1  riastrad const struct intel_dvo_dev_ops tfp410_ops = {
    316  1.1  riastrad 	.init = tfp410_init,
    317  1.1  riastrad 	.detect = tfp410_detect,
    318  1.1  riastrad 	.mode_valid = tfp410_mode_valid,
    319  1.1  riastrad 	.mode_set = tfp410_mode_set,
    320  1.1  riastrad 	.dpms = tfp410_dpms,
    321  1.1  riastrad 	.get_hw_state = tfp410_get_hw_state,
    322  1.1  riastrad 	.dump_regs = tfp410_dump_regs,
    323  1.1  riastrad 	.destroy = tfp410_destroy,
    324  1.1  riastrad };
    325