1*ddadc87fSThomas Zimmermann /* 2*ddadc87fSThomas Zimmermann * Copyright 2012 Red Hat Inc. 3*ddadc87fSThomas Zimmermann * 4*ddadc87fSThomas Zimmermann * Permission is hereby granted, free of charge, to any person obtaining a 5*ddadc87fSThomas Zimmermann * copy of this software and associated documentation files (the 6*ddadc87fSThomas Zimmermann * "Software"), to deal in the Software without restriction, including 7*ddadc87fSThomas Zimmermann * without limitation the rights to use, copy, modify, merge, publish, 8*ddadc87fSThomas Zimmermann * distribute, sub license, and/or sell copies of the Software, and to 9*ddadc87fSThomas Zimmermann * permit persons to whom the Software is furnished to do so, subject to 10*ddadc87fSThomas Zimmermann * the following conditions: 11*ddadc87fSThomas Zimmermann * 12*ddadc87fSThomas Zimmermann * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 13*ddadc87fSThomas Zimmermann * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 14*ddadc87fSThomas Zimmermann * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 15*ddadc87fSThomas Zimmermann * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 16*ddadc87fSThomas Zimmermann * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 17*ddadc87fSThomas Zimmermann * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 18*ddadc87fSThomas Zimmermann * USE OR OTHER DEALINGS IN THE SOFTWARE. 19*ddadc87fSThomas Zimmermann * 20*ddadc87fSThomas Zimmermann * The above copyright notice and this permission notice (including the 21*ddadc87fSThomas Zimmermann * next paragraph) shall be included in all copies or substantial portions 22*ddadc87fSThomas Zimmermann * of the Software. 23*ddadc87fSThomas Zimmermann * 24*ddadc87fSThomas Zimmermann */ 25*ddadc87fSThomas Zimmermann /* 26*ddadc87fSThomas Zimmermann * Authors: Dave Airlie <airlied@redhat.com> 27*ddadc87fSThomas Zimmermann */ 28*ddadc87fSThomas Zimmermann 29*ddadc87fSThomas Zimmermann #include <linux/export.h> 30*ddadc87fSThomas Zimmermann #include <linux/i2c-algo-bit.h> 31*ddadc87fSThomas Zimmermann #include <linux/i2c.h> 32*ddadc87fSThomas Zimmermann #include <linux/pci.h> 33*ddadc87fSThomas Zimmermann 34*ddadc87fSThomas Zimmermann #include <drm/drm_managed.h> 35*ddadc87fSThomas Zimmermann 36*ddadc87fSThomas Zimmermann #include "mgag200_ddc.h" 37*ddadc87fSThomas Zimmermann #include "mgag200_drv.h" 38*ddadc87fSThomas Zimmermann 39*ddadc87fSThomas Zimmermann struct mgag200_ddc { 40*ddadc87fSThomas Zimmermann struct mga_device *mdev; 41*ddadc87fSThomas Zimmermann 42*ddadc87fSThomas Zimmermann int data; 43*ddadc87fSThomas Zimmermann int clock; 44*ddadc87fSThomas Zimmermann 45*ddadc87fSThomas Zimmermann struct i2c_algo_bit_data bit; 46*ddadc87fSThomas Zimmermann struct i2c_adapter adapter; 47*ddadc87fSThomas Zimmermann }; 48*ddadc87fSThomas Zimmermann 49*ddadc87fSThomas Zimmermann static int mga_i2c_read_gpio(struct mga_device *mdev) 50*ddadc87fSThomas Zimmermann { 51*ddadc87fSThomas Zimmermann WREG8(DAC_INDEX, MGA1064_GEN_IO_DATA); 52*ddadc87fSThomas Zimmermann return RREG8(DAC_DATA); 53*ddadc87fSThomas Zimmermann } 54*ddadc87fSThomas Zimmermann 55*ddadc87fSThomas Zimmermann static void mga_i2c_set_gpio(struct mga_device *mdev, int mask, int val) 56*ddadc87fSThomas Zimmermann { 57*ddadc87fSThomas Zimmermann int tmp; 58*ddadc87fSThomas Zimmermann 59*ddadc87fSThomas Zimmermann WREG8(DAC_INDEX, MGA1064_GEN_IO_CTL); 60*ddadc87fSThomas Zimmermann tmp = (RREG8(DAC_DATA) & mask) | val; 61*ddadc87fSThomas Zimmermann WREG_DAC(MGA1064_GEN_IO_CTL, tmp); 62*ddadc87fSThomas Zimmermann WREG_DAC(MGA1064_GEN_IO_DATA, 0); 63*ddadc87fSThomas Zimmermann } 64*ddadc87fSThomas Zimmermann 65*ddadc87fSThomas Zimmermann static inline void mga_i2c_set(struct mga_device *mdev, int mask, int state) 66*ddadc87fSThomas Zimmermann { 67*ddadc87fSThomas Zimmermann if (state) 68*ddadc87fSThomas Zimmermann state = 0; 69*ddadc87fSThomas Zimmermann else 70*ddadc87fSThomas Zimmermann state = mask; 71*ddadc87fSThomas Zimmermann mga_i2c_set_gpio(mdev, ~mask, state); 72*ddadc87fSThomas Zimmermann } 73*ddadc87fSThomas Zimmermann 74*ddadc87fSThomas Zimmermann static void mga_gpio_setsda(void *data, int state) 75*ddadc87fSThomas Zimmermann { 76*ddadc87fSThomas Zimmermann struct mgag200_ddc *ddc = data; 77*ddadc87fSThomas Zimmermann 78*ddadc87fSThomas Zimmermann mga_i2c_set(ddc->mdev, ddc->data, state); 79*ddadc87fSThomas Zimmermann } 80*ddadc87fSThomas Zimmermann 81*ddadc87fSThomas Zimmermann static void mga_gpio_setscl(void *data, int state) 82*ddadc87fSThomas Zimmermann { 83*ddadc87fSThomas Zimmermann struct mgag200_ddc *ddc = data; 84*ddadc87fSThomas Zimmermann 85*ddadc87fSThomas Zimmermann mga_i2c_set(ddc->mdev, ddc->clock, state); 86*ddadc87fSThomas Zimmermann } 87*ddadc87fSThomas Zimmermann 88*ddadc87fSThomas Zimmermann static int mga_gpio_getsda(void *data) 89*ddadc87fSThomas Zimmermann { 90*ddadc87fSThomas Zimmermann struct mgag200_ddc *ddc = data; 91*ddadc87fSThomas Zimmermann 92*ddadc87fSThomas Zimmermann return (mga_i2c_read_gpio(ddc->mdev) & ddc->data) ? 1 : 0; 93*ddadc87fSThomas Zimmermann } 94*ddadc87fSThomas Zimmermann 95*ddadc87fSThomas Zimmermann static int mga_gpio_getscl(void *data) 96*ddadc87fSThomas Zimmermann { 97*ddadc87fSThomas Zimmermann struct mgag200_ddc *ddc = data; 98*ddadc87fSThomas Zimmermann 99*ddadc87fSThomas Zimmermann return (mga_i2c_read_gpio(ddc->mdev) & ddc->clock) ? 1 : 0; 100*ddadc87fSThomas Zimmermann } 101*ddadc87fSThomas Zimmermann 102*ddadc87fSThomas Zimmermann static void mgag200_ddc_release(struct drm_device *dev, void *res) 103*ddadc87fSThomas Zimmermann { 104*ddadc87fSThomas Zimmermann struct mgag200_ddc *ddc = res; 105*ddadc87fSThomas Zimmermann 106*ddadc87fSThomas Zimmermann i2c_del_adapter(&ddc->adapter); 107*ddadc87fSThomas Zimmermann } 108*ddadc87fSThomas Zimmermann 109*ddadc87fSThomas Zimmermann struct i2c_adapter *mgag200_ddc_create(struct mga_device *mdev) 110*ddadc87fSThomas Zimmermann { 111*ddadc87fSThomas Zimmermann struct drm_device *dev = &mdev->base; 112*ddadc87fSThomas Zimmermann const struct mgag200_device_info *info = mdev->info; 113*ddadc87fSThomas Zimmermann struct mgag200_ddc *ddc; 114*ddadc87fSThomas Zimmermann struct i2c_algo_bit_data *bit; 115*ddadc87fSThomas Zimmermann struct i2c_adapter *adapter; 116*ddadc87fSThomas Zimmermann int ret; 117*ddadc87fSThomas Zimmermann 118*ddadc87fSThomas Zimmermann ddc = drmm_kzalloc(dev, sizeof(*ddc), GFP_KERNEL); 119*ddadc87fSThomas Zimmermann if (!ddc) 120*ddadc87fSThomas Zimmermann return ERR_PTR(-ENOMEM); 121*ddadc87fSThomas Zimmermann 122*ddadc87fSThomas Zimmermann WREG_DAC(MGA1064_GEN_IO_CTL2, 1); 123*ddadc87fSThomas Zimmermann WREG_DAC(MGA1064_GEN_IO_DATA, 0xff); 124*ddadc87fSThomas Zimmermann WREG_DAC(MGA1064_GEN_IO_CTL, 0); 125*ddadc87fSThomas Zimmermann 126*ddadc87fSThomas Zimmermann ddc->mdev = mdev; 127*ddadc87fSThomas Zimmermann ddc->data = BIT(info->i2c.data_bit); 128*ddadc87fSThomas Zimmermann ddc->clock = BIT(info->i2c.clock_bit); 129*ddadc87fSThomas Zimmermann 130*ddadc87fSThomas Zimmermann bit = &ddc->bit; 131*ddadc87fSThomas Zimmermann bit->data = ddc; 132*ddadc87fSThomas Zimmermann bit->setsda = mga_gpio_setsda; 133*ddadc87fSThomas Zimmermann bit->setscl = mga_gpio_setscl; 134*ddadc87fSThomas Zimmermann bit->getsda = mga_gpio_getsda; 135*ddadc87fSThomas Zimmermann bit->getscl = mga_gpio_getscl; 136*ddadc87fSThomas Zimmermann bit->udelay = 10; 137*ddadc87fSThomas Zimmermann bit->timeout = usecs_to_jiffies(2200); 138*ddadc87fSThomas Zimmermann 139*ddadc87fSThomas Zimmermann adapter = &ddc->adapter; 140*ddadc87fSThomas Zimmermann adapter->owner = THIS_MODULE; 141*ddadc87fSThomas Zimmermann adapter->algo_data = bit; 142*ddadc87fSThomas Zimmermann adapter->dev.parent = dev->dev; 143*ddadc87fSThomas Zimmermann snprintf(adapter->name, sizeof(adapter->name), "Matrox DDC bus"); 144*ddadc87fSThomas Zimmermann i2c_set_adapdata(adapter, ddc); 145*ddadc87fSThomas Zimmermann 146*ddadc87fSThomas Zimmermann ret = i2c_bit_add_bus(adapter); 147*ddadc87fSThomas Zimmermann if (ret) 148*ddadc87fSThomas Zimmermann return ERR_PTR(ret); 149*ddadc87fSThomas Zimmermann 150*ddadc87fSThomas Zimmermann ret = drmm_add_action_or_reset(dev, mgag200_ddc_release, ddc); 151*ddadc87fSThomas Zimmermann if (ret) 152*ddadc87fSThomas Zimmermann return ERR_PTR(ret); 153*ddadc87fSThomas Zimmermann 154*ddadc87fSThomas Zimmermann return adapter; 155*ddadc87fSThomas Zimmermann } 156