Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright © 2006-2007 Intel Corporation
0004  *
0005  * Authors:
0006  *  Eric Anholt <eric@anholt.net>
0007  */
0008 
0009 #include <linux/delay.h>
0010 #include <linux/export.h>
0011 #include <linux/i2c-algo-bit.h>
0012 #include <linux/i2c.h>
0013 
0014 #include "psb_drv.h"
0015 #include "psb_intel_reg.h"
0016 
0017 /*
0018  * Intel GPIO access functions
0019  */
0020 
0021 #define I2C_RISEFALL_TIME 20
0022 
0023 static int get_clock(void *data)
0024 {
0025     struct gma_i2c_chan *chan = data;
0026     struct drm_device *dev = chan->drm_dev;
0027     u32 val;
0028 
0029     val = REG_READ(chan->reg);
0030     return (val & GPIO_CLOCK_VAL_IN) != 0;
0031 }
0032 
0033 static int get_data(void *data)
0034 {
0035     struct gma_i2c_chan *chan = data;
0036     struct drm_device *dev = chan->drm_dev;
0037     u32 val;
0038 
0039     val = REG_READ(chan->reg);
0040     return (val & GPIO_DATA_VAL_IN) != 0;
0041 }
0042 
0043 static void set_clock(void *data, int state_high)
0044 {
0045     struct gma_i2c_chan *chan = data;
0046     struct drm_device *dev = chan->drm_dev;
0047     u32 reserved = 0, clock_bits;
0048 
0049     /* On most chips, these bits must be preserved in software. */
0050     reserved =
0051             REG_READ(chan->reg) & (GPIO_DATA_PULLUP_DISABLE |
0052                        GPIO_CLOCK_PULLUP_DISABLE);
0053 
0054     if (state_high)
0055         clock_bits = GPIO_CLOCK_DIR_IN | GPIO_CLOCK_DIR_MASK;
0056     else
0057         clock_bits = GPIO_CLOCK_DIR_OUT | GPIO_CLOCK_DIR_MASK |
0058             GPIO_CLOCK_VAL_MASK;
0059     REG_WRITE(chan->reg, reserved | clock_bits);
0060     udelay(I2C_RISEFALL_TIME);  /* wait for the line to change state */
0061 }
0062 
0063 static void set_data(void *data, int state_high)
0064 {
0065     struct gma_i2c_chan *chan = data;
0066     struct drm_device *dev = chan->drm_dev;
0067     u32 reserved = 0, data_bits;
0068 
0069     /* On most chips, these bits must be preserved in software. */
0070     reserved =
0071             REG_READ(chan->reg) & (GPIO_DATA_PULLUP_DISABLE |
0072                        GPIO_CLOCK_PULLUP_DISABLE);
0073 
0074     if (state_high)
0075         data_bits = GPIO_DATA_DIR_IN | GPIO_DATA_DIR_MASK;
0076     else
0077         data_bits =
0078             GPIO_DATA_DIR_OUT | GPIO_DATA_DIR_MASK |
0079             GPIO_DATA_VAL_MASK;
0080 
0081     REG_WRITE(chan->reg, reserved | data_bits);
0082     udelay(I2C_RISEFALL_TIME);  /* wait for the line to change state */
0083 }
0084 
0085 /**
0086  * gma_i2c_create - instantiate an Intel i2c bus using the specified GPIO reg
0087  * @dev: DRM device
0088  * @reg: GPIO reg to use
0089  * @name: name for this bus
0090  *
0091  * Creates and registers a new i2c bus with the Linux i2c layer, for use
0092  * in output probing and control (e.g. DDC or SDVO control functions).
0093  *
0094  * Possible values for @reg include:
0095  *   %GPIOA
0096  *   %GPIOB
0097  *   %GPIOC
0098  *   %GPIOD
0099  *   %GPIOE
0100  *   %GPIOF
0101  *   %GPIOG
0102  *   %GPIOH
0103  * see PRM for details on how these different busses are used.
0104  */
0105 struct gma_i2c_chan *gma_i2c_create(struct drm_device *dev, const u32 reg,
0106                     const char *name)
0107 {
0108     struct gma_i2c_chan *chan;
0109 
0110     chan = kzalloc(sizeof(struct gma_i2c_chan), GFP_KERNEL);
0111     if (!chan)
0112         goto out_free;
0113 
0114     chan->drm_dev = dev;
0115     chan->reg = reg;
0116     snprintf(chan->base.name, I2C_NAME_SIZE, "intel drm %s", name);
0117     chan->base.owner = THIS_MODULE;
0118     chan->base.algo_data = &chan->algo;
0119     chan->base.dev.parent = dev->dev;
0120     chan->algo.setsda = set_data;
0121     chan->algo.setscl = set_clock;
0122     chan->algo.getsda = get_data;
0123     chan->algo.getscl = get_clock;
0124     chan->algo.udelay = 20;
0125     chan->algo.timeout = usecs_to_jiffies(2200);
0126     chan->algo.data = chan;
0127 
0128     i2c_set_adapdata(&chan->base, chan);
0129 
0130     if (i2c_bit_add_bus(&chan->base))
0131         goto out_free;
0132 
0133     /* JJJ:  raise SCL and SDA? */
0134     set_data(chan, 1);
0135     set_clock(chan, 1);
0136     udelay(20);
0137 
0138     return chan;
0139 
0140 out_free:
0141     kfree(chan);
0142     return NULL;
0143 }
0144 
0145 /**
0146  * gma_i2c_destroy - unregister and free i2c bus resources
0147  * @chan: channel to free
0148  *
0149  * Unregister the adapter from the i2c layer, then free the structure.
0150  */
0151 void gma_i2c_destroy(struct gma_i2c_chan *chan)
0152 {
0153     if (!chan)
0154         return;
0155 
0156     i2c_del_adapter(&chan->base);
0157     kfree(chan);
0158 }