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