Merge tag 'io_uring-5.11-2021-01-16' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / gpu / drm / mgag200 / mgag200_i2c.c
blob09731e614e46d72b487ed2aca62b3509a74c5f40
1 /*
2 * Copyright 2012 Red Hat Inc.
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:
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.
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.
26 * Authors: Dave Airlie <airlied@redhat.com>
29 #include <linux/export.h>
30 #include <linux/i2c-algo-bit.h>
31 #include <linux/i2c.h>
32 #include <linux/pci.h>
34 #include "mgag200_drv.h"
36 static int mga_i2c_read_gpio(struct mga_device *mdev)
38 WREG8(DAC_INDEX, MGA1064_GEN_IO_DATA);
39 return RREG8(DAC_DATA);
42 static void mga_i2c_set_gpio(struct mga_device *mdev, int mask, int val)
44 int tmp;
46 WREG8(DAC_INDEX, MGA1064_GEN_IO_CTL);
47 tmp = (RREG8(DAC_DATA) & mask) | val;
48 WREG_DAC(MGA1064_GEN_IO_CTL, tmp);
49 WREG_DAC(MGA1064_GEN_IO_DATA, 0);
52 static inline void mga_i2c_set(struct mga_device *mdev, int mask, int state)
54 if (state)
55 state = 0;
56 else
57 state = mask;
58 mga_i2c_set_gpio(mdev, ~mask, state);
61 static void mga_gpio_setsda(void *data, int state)
63 struct mga_i2c_chan *i2c = data;
64 struct mga_device *mdev = to_mga_device(i2c->dev);
65 mga_i2c_set(mdev, i2c->data, state);
68 static void mga_gpio_setscl(void *data, int state)
70 struct mga_i2c_chan *i2c = data;
71 struct mga_device *mdev = to_mga_device(i2c->dev);
72 mga_i2c_set(mdev, i2c->clock, state);
75 static int mga_gpio_getsda(void *data)
77 struct mga_i2c_chan *i2c = data;
78 struct mga_device *mdev = to_mga_device(i2c->dev);
79 return (mga_i2c_read_gpio(mdev) & i2c->data) ? 1 : 0;
82 static int mga_gpio_getscl(void *data)
84 struct mga_i2c_chan *i2c = data;
85 struct mga_device *mdev = to_mga_device(i2c->dev);
86 return (mga_i2c_read_gpio(mdev) & i2c->clock) ? 1 : 0;
89 struct mga_i2c_chan *mgag200_i2c_create(struct drm_device *dev)
91 struct mga_device *mdev = to_mga_device(dev);
92 struct mga_i2c_chan *i2c;
93 int ret;
94 int data, clock;
96 WREG_DAC(MGA1064_GEN_IO_CTL2, 1);
97 WREG_DAC(MGA1064_GEN_IO_DATA, 0xff);
98 WREG_DAC(MGA1064_GEN_IO_CTL, 0);
100 switch (mdev->type) {
101 case G200_SE_A:
102 case G200_SE_B:
103 case G200_EV:
104 case G200_WB:
105 case G200_EW3:
106 data = 1;
107 clock = 2;
108 break;
109 case G200_EH:
110 case G200_EH3:
111 case G200_ER:
112 data = 2;
113 clock = 1;
114 break;
115 default:
116 data = 2;
117 clock = 8;
118 break;
121 i2c = kzalloc(sizeof(struct mga_i2c_chan), GFP_KERNEL);
122 if (!i2c)
123 return NULL;
125 i2c->data = data;
126 i2c->clock = clock;
127 i2c->adapter.owner = THIS_MODULE;
128 i2c->adapter.class = I2C_CLASS_DDC;
129 i2c->adapter.dev.parent = &dev->pdev->dev;
130 i2c->dev = dev;
131 i2c_set_adapdata(&i2c->adapter, i2c);
132 snprintf(i2c->adapter.name, sizeof(i2c->adapter.name), "mga i2c");
134 i2c->adapter.algo_data = &i2c->bit;
136 i2c->bit.udelay = 10;
137 i2c->bit.timeout = 2;
138 i2c->bit.data = i2c;
139 i2c->bit.setsda = mga_gpio_setsda;
140 i2c->bit.setscl = mga_gpio_setscl;
141 i2c->bit.getsda = mga_gpio_getsda;
142 i2c->bit.getscl = mga_gpio_getscl;
144 ret = i2c_bit_add_bus(&i2c->adapter);
145 if (ret) {
146 kfree(i2c);
147 i2c = NULL;
149 return i2c;
152 void mgag200_i2c_destroy(struct mga_i2c_chan *i2c)
154 if (!i2c)
155 return;
156 i2c_del_adapter(&i2c->adapter);
157 kfree(i2c);