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_drv.h"
37
mga_i2c_read_gpio(struct mga_device * mdev)38 static int mga_i2c_read_gpio(struct mga_device *mdev)
39 {
40 WREG8(DAC_INDEX, MGA1064_GEN_IO_DATA);
41 return RREG8(DAC_DATA);
42 }
43
mga_i2c_set_gpio(struct mga_device * mdev,int mask,int val)44 static void mga_i2c_set_gpio(struct mga_device *mdev, int mask, int val)
45 {
46 int tmp;
47
48 WREG8(DAC_INDEX, MGA1064_GEN_IO_CTL);
49 tmp = (RREG8(DAC_DATA) & mask) | val;
50 WREG_DAC(MGA1064_GEN_IO_CTL, tmp);
51 WREG_DAC(MGA1064_GEN_IO_DATA, 0);
52 }
53
mga_i2c_set(struct mga_device * mdev,int mask,int state)54 static inline void mga_i2c_set(struct mga_device *mdev, int mask, int state)
55 {
56 if (state)
57 state = 0;
58 else
59 state = mask;
60 mga_i2c_set_gpio(mdev, ~mask, state);
61 }
62
mga_gpio_setsda(void * data,int state)63 static void mga_gpio_setsda(void *data, int state)
64 {
65 struct mga_i2c_chan *i2c = data;
66 struct mga_device *mdev = to_mga_device(i2c->dev);
67 mga_i2c_set(mdev, i2c->data, state);
68 }
69
mga_gpio_setscl(void * data,int state)70 static void mga_gpio_setscl(void *data, int state)
71 {
72 struct mga_i2c_chan *i2c = data;
73 struct mga_device *mdev = to_mga_device(i2c->dev);
74 mga_i2c_set(mdev, i2c->clock, state);
75 }
76
mga_gpio_getsda(void * data)77 static int mga_gpio_getsda(void *data)
78 {
79 struct mga_i2c_chan *i2c = data;
80 struct mga_device *mdev = to_mga_device(i2c->dev);
81 return (mga_i2c_read_gpio(mdev) & i2c->data) ? 1 : 0;
82 }
83
mga_gpio_getscl(void * data)84 static int mga_gpio_getscl(void *data)
85 {
86 struct mga_i2c_chan *i2c = data;
87 struct mga_device *mdev = to_mga_device(i2c->dev);
88 return (mga_i2c_read_gpio(mdev) & i2c->clock) ? 1 : 0;
89 }
90
mgag200_i2c_release(struct drm_device * dev,void * res)91 static void mgag200_i2c_release(struct drm_device *dev, void *res)
92 {
93 struct mga_i2c_chan *i2c = res;
94
95 i2c_del_adapter(&i2c->adapter);
96 }
97
mgag200_i2c_init(struct mga_device * mdev,struct mga_i2c_chan * i2c)98 int mgag200_i2c_init(struct mga_device *mdev, struct mga_i2c_chan *i2c)
99 {
100 struct drm_device *dev = &mdev->base;
101 const struct mgag200_device_info *info = mdev->info;
102 int ret;
103
104 WREG_DAC(MGA1064_GEN_IO_CTL2, 1);
105 WREG_DAC(MGA1064_GEN_IO_DATA, 0xff);
106 WREG_DAC(MGA1064_GEN_IO_CTL, 0);
107
108 i2c->data = BIT(info->i2c.data_bit);
109 i2c->clock = BIT(info->i2c.clock_bit);
110 i2c->adapter.owner = THIS_MODULE;
111 i2c->adapter.class = I2C_CLASS_DDC;
112 i2c->adapter.dev.parent = dev->dev;
113 i2c->dev = dev;
114 i2c_set_adapdata(&i2c->adapter, i2c);
115 snprintf(i2c->adapter.name, sizeof(i2c->adapter.name), "mga i2c");
116
117 i2c->adapter.algo_data = &i2c->bit;
118
119 i2c->bit.udelay = 10;
120 i2c->bit.timeout = usecs_to_jiffies(2200);
121 i2c->bit.data = i2c;
122 i2c->bit.setsda = mga_gpio_setsda;
123 i2c->bit.setscl = mga_gpio_setscl;
124 i2c->bit.getsda = mga_gpio_getsda;
125 i2c->bit.getscl = mga_gpio_getscl;
126
127 ret = i2c_bit_add_bus(&i2c->adapter);
128 if (ret)
129 return ret;
130
131 return drmm_add_action_or_reset(dev, mgag200_i2c_release, i2c);
132 }
133