Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
mgag200_i2c.c
Go to the documentation of this file.
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 <[email protected]>
27  */
28 #include <linux/export.h>
29 #include <linux/i2c.h>
30 #include <linux/i2c-algo-bit.h>
31 #include <drm/drmP.h>
32 
33 #include "mgag200_drv.h"
34 
35 static int mga_i2c_read_gpio(struct mga_device *mdev)
36 {
38  return RREG8(DAC_DATA);
39 }
40 
41 static void mga_i2c_set_gpio(struct mga_device *mdev, int mask, int val)
42 {
43  int tmp;
44 
46  tmp = (RREG8(DAC_DATA) & mask) | val;
49 }
50 
51 static inline void mga_i2c_set(struct mga_device *mdev, int mask, int state)
52 {
53  if (state)
54  state = 0;
55  else
56  state = mask;
57  mga_i2c_set_gpio(mdev, ~mask, state);
58 }
59 
60 static void mga_gpio_setsda(void *data, int state)
61 {
62  struct mga_i2c_chan *i2c = data;
63  struct mga_device *mdev = i2c->dev->dev_private;
64  mga_i2c_set(mdev, i2c->data, state);
65 }
66 
67 static void mga_gpio_setscl(void *data, int state)
68 {
69  struct mga_i2c_chan *i2c = data;
70  struct mga_device *mdev = i2c->dev->dev_private;
71  mga_i2c_set(mdev, i2c->clock, state);
72 }
73 
74 static int mga_gpio_getsda(void *data)
75 {
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;
79 }
80 
81 static int mga_gpio_getscl(void *data)
82 {
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;
86 }
87 
89 {
90  struct mga_device *mdev = dev->dev_private;
91  struct mga_i2c_chan *i2c;
92  int ret;
93  int data, clock;
94 
97 
98  switch (mdev->type) {
99  case G200_SE_A:
100  case G200_SE_B:
101  case G200_EV:
102  case G200_WB:
103  data = 1;
104  clock = 2;
105  break;
106  case G200_EH:
107  case G200_ER:
108  data = 2;
109  clock = 1;
110  break;
111  default:
112  data = 2;
113  clock = 8;
114  break;
115  }
116 
117  i2c = kzalloc(sizeof(struct mga_i2c_chan), GFP_KERNEL);
118  if (!i2c)
119  return NULL;
120 
121  i2c->data = data;
122  i2c->clock = clock;
123  i2c->adapter.owner = THIS_MODULE;
124  i2c->adapter.class = I2C_CLASS_DDC;
125  i2c->adapter.dev.parent = &dev->pdev->dev;
126  i2c->dev = dev;
127  i2c_set_adapdata(&i2c->adapter, i2c);
128  snprintf(i2c->adapter.name, sizeof(i2c->adapter.name), "mga i2c");
129 
130  i2c->adapter.algo_data = &i2c->bit;
131 
132  i2c->bit.udelay = 10;
133  i2c->bit.timeout = 2;
134  i2c->bit.data = i2c;
135  i2c->bit.setsda = mga_gpio_setsda;
136  i2c->bit.setscl = mga_gpio_setscl;
137  i2c->bit.getsda = mga_gpio_getsda;
138  i2c->bit.getscl = mga_gpio_getscl;
139 
140  ret = i2c_bit_add_bus(&i2c->adapter);
141  if (ret) {
142  kfree(i2c);
143  i2c = NULL;
144  }
145  return i2c;
146 }
147 
149 {
150  if (!i2c)
151  return;
152  i2c_del_adapter(&i2c->adapter);
153  kfree(i2c);
154 }
155