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