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
26 * Authors: Dave Airlie <airlied@redhat.com>
28 #include <linux/export.h>
29 #include <linux/i2c.h>
30 #include <linux/i2c-algo-bit.h>
33 #include "mgag200_drv.h"
35 static int mga_i2c_read_gpio(struct mga_device
*mdev
)
37 WREG8(DAC_INDEX
, MGA1064_GEN_IO_DATA
);
38 return RREG8(DAC_DATA
);
41 static void mga_i2c_set_gpio(struct mga_device
*mdev
, int mask
, int val
)
45 WREG8(DAC_INDEX
, MGA1064_GEN_IO_CTL
);
46 tmp
= (RREG8(DAC_DATA
) & mask
) | val
;
47 WREG_DAC(MGA1064_GEN_IO_CTL
, tmp
);
48 WREG_DAC(MGA1064_GEN_IO_DATA
, 0);
51 static inline void mga_i2c_set(struct mga_device
*mdev
, int mask
, int state
)
57 mga_i2c_set_gpio(mdev
, ~mask
, state
);
60 static void mga_gpio_setsda(void *data
, int state
)
62 struct mga_i2c_chan
*i2c
= data
;
63 struct mga_device
*mdev
= i2c
->dev
->dev_private
;
64 mga_i2c_set(mdev
, i2c
->data
, state
);
67 static void mga_gpio_setscl(void *data
, int state
)
69 struct mga_i2c_chan
*i2c
= data
;
70 struct mga_device
*mdev
= i2c
->dev
->dev_private
;
71 mga_i2c_set(mdev
, i2c
->clock
, state
);
74 static int mga_gpio_getsda(void *data
)
76 struct mga_i2c_chan
*i2c
= data
;
77 struct mga_device
*mdev
= i2c
->dev
->dev_private
;
78 return (mga_i2c_read_gpio(mdev
) & i2c
->data
) ? 1 : 0;
81 static int mga_gpio_getscl(void *data
)
83 struct mga_i2c_chan
*i2c
= data
;
84 struct mga_device
*mdev
= i2c
->dev
->dev_private
;
85 return (mga_i2c_read_gpio(mdev
) & i2c
->clock
) ? 1 : 0;
88 struct mga_i2c_chan
*mgag200_i2c_create(struct drm_device
*dev
)
90 struct mga_device
*mdev
= dev
->dev_private
;
91 struct mga_i2c_chan
*i2c
;
95 WREG_DAC(MGA1064_GEN_IO_CTL2
, 1);
96 WREG_DAC(MGA1064_GEN_IO_DATA
, 0xff);
97 WREG_DAC(MGA1064_GEN_IO_CTL
, 0);
119 i2c
= kzalloc(sizeof(struct mga_i2c_chan
), GFP_KERNEL
);
125 i2c
->adapter
.owner
= THIS_MODULE
;
126 i2c
->adapter
.class = I2C_CLASS_DDC
;
127 i2c
->adapter
.dev
.parent
= &dev
->pdev
->dev
;
129 i2c_set_adapdata(&i2c
->adapter
, i2c
);
130 snprintf(i2c
->adapter
.name
, sizeof(i2c
->adapter
.name
), "mga i2c");
132 i2c
->adapter
.algo_data
= &i2c
->bit
;
134 i2c
->bit
.udelay
= 10;
135 i2c
->bit
.timeout
= 2;
137 i2c
->bit
.setsda
= mga_gpio_setsda
;
138 i2c
->bit
.setscl
= mga_gpio_setscl
;
139 i2c
->bit
.getsda
= mga_gpio_getsda
;
140 i2c
->bit
.getscl
= mga_gpio_getscl
;
142 ret
= i2c_bit_add_bus(&i2c
->adapter
);
150 void mgag200_i2c_destroy(struct mga_i2c_chan
*i2c
)
154 i2c_del_adapter(&i2c
->adapter
);