xref: /openbmc/linux/drivers/gpu/drm/gma500/intel_i2c.c (revision 2f0f2441b4a10948e2ec042b48fef13680387f7c)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright © 2006-2007 Intel Corporation
4  *
5  * Authors:
6  *	Eric Anholt <eric@anholt.net>
7  */
8 #include <linux/export.h>
9 #include <linux/i2c.h>
10 #include <linux/i2c-algo-bit.h>
11 
12 #include "psb_drv.h"
13 #include "psb_intel_reg.h"
14 
15 /*
16  * Intel GPIO access functions
17  */
18 
19 #define I2C_RISEFALL_TIME 20
20 
21 static int get_clock(void *data)
22 {
23 	struct psb_intel_i2c_chan *chan = data;
24 	struct drm_device *dev = chan->drm_dev;
25 	u32 val;
26 
27 	val = REG_READ(chan->reg);
28 	return (val & GPIO_CLOCK_VAL_IN) != 0;
29 }
30 
31 static int get_data(void *data)
32 {
33 	struct psb_intel_i2c_chan *chan = data;
34 	struct drm_device *dev = chan->drm_dev;
35 	u32 val;
36 
37 	val = REG_READ(chan->reg);
38 	return (val & GPIO_DATA_VAL_IN) != 0;
39 }
40 
41 static void set_clock(void *data, int state_high)
42 {
43 	struct psb_intel_i2c_chan *chan = data;
44 	struct drm_device *dev = chan->drm_dev;
45 	u32 reserved = 0, clock_bits;
46 
47 	/* On most chips, these bits must be preserved in software. */
48 	reserved =
49 		    REG_READ(chan->reg) & (GPIO_DATA_PULLUP_DISABLE |
50 					   GPIO_CLOCK_PULLUP_DISABLE);
51 
52 	if (state_high)
53 		clock_bits = GPIO_CLOCK_DIR_IN | GPIO_CLOCK_DIR_MASK;
54 	else
55 		clock_bits = GPIO_CLOCK_DIR_OUT | GPIO_CLOCK_DIR_MASK |
56 		    GPIO_CLOCK_VAL_MASK;
57 	REG_WRITE(chan->reg, reserved | clock_bits);
58 	udelay(I2C_RISEFALL_TIME);	/* wait for the line to change state */
59 }
60 
61 static void set_data(void *data, int state_high)
62 {
63 	struct psb_intel_i2c_chan *chan = data;
64 	struct drm_device *dev = chan->drm_dev;
65 	u32 reserved = 0, data_bits;
66 
67 	/* On most chips, these bits must be preserved in software. */
68 	reserved =
69 		    REG_READ(chan->reg) & (GPIO_DATA_PULLUP_DISABLE |
70 					   GPIO_CLOCK_PULLUP_DISABLE);
71 
72 	if (state_high)
73 		data_bits = GPIO_DATA_DIR_IN | GPIO_DATA_DIR_MASK;
74 	else
75 		data_bits =
76 		    GPIO_DATA_DIR_OUT | GPIO_DATA_DIR_MASK |
77 		    GPIO_DATA_VAL_MASK;
78 
79 	REG_WRITE(chan->reg, reserved | data_bits);
80 	udelay(I2C_RISEFALL_TIME);	/* wait for the line to change state */
81 }
82 
83 /**
84  * psb_intel_i2c_create - instantiate an Intel i2c bus using the specified GPIO reg
85  * @dev: DRM device
86  * @output: driver specific output device
87  * @reg: GPIO reg to use
88  * @name: name for this bus
89  *
90  * Creates and registers a new i2c bus with the Linux i2c layer, for use
91  * in output probing and control (e.g. DDC or SDVO control functions).
92  *
93  * Possible values for @reg include:
94  *   %GPIOA
95  *   %GPIOB
96  *   %GPIOC
97  *   %GPIOD
98  *   %GPIOE
99  *   %GPIOF
100  *   %GPIOG
101  *   %GPIOH
102  * see PRM for details on how these different busses are used.
103  */
104 struct psb_intel_i2c_chan *psb_intel_i2c_create(struct drm_device *dev,
105 					const u32 reg, const char *name)
106 {
107 	struct psb_intel_i2c_chan *chan;
108 
109 	chan = kzalloc(sizeof(struct psb_intel_i2c_chan), GFP_KERNEL);
110 	if (!chan)
111 		goto out_free;
112 
113 	chan->drm_dev = dev;
114 	chan->reg = reg;
115 	snprintf(chan->adapter.name, I2C_NAME_SIZE, "intel drm %s", name);
116 	chan->adapter.owner = THIS_MODULE;
117 	chan->adapter.algo_data = &chan->algo;
118 	chan->adapter.dev.parent = &dev->pdev->dev;
119 	chan->algo.setsda = set_data;
120 	chan->algo.setscl = set_clock;
121 	chan->algo.getsda = get_data;
122 	chan->algo.getscl = get_clock;
123 	chan->algo.udelay = 20;
124 	chan->algo.timeout = usecs_to_jiffies(2200);
125 	chan->algo.data = chan;
126 
127 	i2c_set_adapdata(&chan->adapter, chan);
128 
129 	if (i2c_bit_add_bus(&chan->adapter))
130 		goto out_free;
131 
132 	/* JJJ:  raise SCL and SDA? */
133 	set_data(chan, 1);
134 	set_clock(chan, 1);
135 	udelay(20);
136 
137 	return chan;
138 
139 out_free:
140 	kfree(chan);
141 	return NULL;
142 }
143 
144 /**
145  * psb_intel_i2c_destroy - unregister and free i2c bus resources
146  * @output: channel to free
147  *
148  * Unregister the adapter from the i2c layer, then free the structure.
149  */
150 void psb_intel_i2c_destroy(struct psb_intel_i2c_chan *chan)
151 {
152 	if (!chan)
153 		return;
154 
155 	i2c_del_adapter(&chan->adapter);
156 	kfree(chan);
157 }
158