1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright © 2006-2007 Intel Corporation
9 #include <linux/delay.h>
10 #include <linux/export.h>
11 #include <linux/i2c-algo-bit.h>
12 #include <linux/i2c.h>
15 #include "psb_intel_reg.h"
18 * Intel GPIO access functions
21 #define I2C_RISEFALL_TIME 20
23 static int get_clock(void *data)
25 struct psb_intel_i2c_chan *chan = data;
26 struct drm_device *dev = chan->drm_dev;
29 val = REG_READ(chan->reg);
30 return (val & GPIO_CLOCK_VAL_IN) != 0;
33 static int get_data(void *data)
35 struct psb_intel_i2c_chan *chan = data;
36 struct drm_device *dev = chan->drm_dev;
39 val = REG_READ(chan->reg);
40 return (val & GPIO_DATA_VAL_IN) != 0;
43 static void set_clock(void *data, int state_high)
45 struct psb_intel_i2c_chan *chan = data;
46 struct drm_device *dev = chan->drm_dev;
47 u32 reserved = 0, clock_bits;
49 /* On most chips, these bits must be preserved in software. */
51 REG_READ(chan->reg) & (GPIO_DATA_PULLUP_DISABLE |
52 GPIO_CLOCK_PULLUP_DISABLE);
55 clock_bits = GPIO_CLOCK_DIR_IN | GPIO_CLOCK_DIR_MASK;
57 clock_bits = GPIO_CLOCK_DIR_OUT | GPIO_CLOCK_DIR_MASK |
59 REG_WRITE(chan->reg, reserved | clock_bits);
60 udelay(I2C_RISEFALL_TIME); /* wait for the line to change state */
63 static void set_data(void *data, int state_high)
65 struct psb_intel_i2c_chan *chan = data;
66 struct drm_device *dev = chan->drm_dev;
67 u32 reserved = 0, data_bits;
69 /* On most chips, these bits must be preserved in software. */
71 REG_READ(chan->reg) & (GPIO_DATA_PULLUP_DISABLE |
72 GPIO_CLOCK_PULLUP_DISABLE);
75 data_bits = GPIO_DATA_DIR_IN | GPIO_DATA_DIR_MASK;
78 GPIO_DATA_DIR_OUT | GPIO_DATA_DIR_MASK |
81 REG_WRITE(chan->reg, reserved | data_bits);
82 udelay(I2C_RISEFALL_TIME); /* wait for the line to change state */
86 * psb_intel_i2c_create - instantiate an Intel i2c bus using the specified GPIO reg
88 * @output: driver specific output device
89 * @reg: GPIO reg to use
90 * @name: name for this bus
92 * Creates and registers a new i2c bus with the Linux i2c layer, for use
93 * in output probing and control (e.g. DDC or SDVO control functions).
95 * Possible values for @reg include:
104 * see PRM for details on how these different busses are used.
106 struct psb_intel_i2c_chan *psb_intel_i2c_create(struct drm_device *dev,
107 const u32 reg, const char *name)
109 struct psb_intel_i2c_chan *chan;
111 chan = kzalloc(sizeof(struct psb_intel_i2c_chan), GFP_KERNEL);
117 snprintf(chan->adapter.name, I2C_NAME_SIZE, "intel drm %s", name);
118 chan->adapter.owner = THIS_MODULE;
119 chan->adapter.algo_data = &chan->algo;
120 chan->adapter.dev.parent = &dev->pdev->dev;
121 chan->algo.setsda = set_data;
122 chan->algo.setscl = set_clock;
123 chan->algo.getsda = get_data;
124 chan->algo.getscl = get_clock;
125 chan->algo.udelay = 20;
126 chan->algo.timeout = usecs_to_jiffies(2200);
127 chan->algo.data = chan;
129 i2c_set_adapdata(&chan->adapter, chan);
131 if (i2c_bit_add_bus(&chan->adapter))
134 /* JJJ: raise SCL and SDA? */
147 * psb_intel_i2c_destroy - unregister and free i2c bus resources
148 * @output: channel to free
150 * Unregister the adapter from the i2c layer, then free the structure.
152 void psb_intel_i2c_destroy(struct psb_intel_i2c_chan *chan)
157 i2c_del_adapter(&chan->adapter);