18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright © 2006-2007 Intel Corporation
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Authors:
68c2ecf20Sopenharmony_ci *	Eric Anholt <eric@anholt.net>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/delay.h>
108c2ecf20Sopenharmony_ci#include <linux/export.h>
118c2ecf20Sopenharmony_ci#include <linux/i2c-algo-bit.h>
128c2ecf20Sopenharmony_ci#include <linux/i2c.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include "psb_drv.h"
158c2ecf20Sopenharmony_ci#include "psb_intel_reg.h"
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci/*
188c2ecf20Sopenharmony_ci * Intel GPIO access functions
198c2ecf20Sopenharmony_ci */
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#define I2C_RISEFALL_TIME 20
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_cistatic int get_clock(void *data)
248c2ecf20Sopenharmony_ci{
258c2ecf20Sopenharmony_ci	struct psb_intel_i2c_chan *chan = data;
268c2ecf20Sopenharmony_ci	struct drm_device *dev = chan->drm_dev;
278c2ecf20Sopenharmony_ci	u32 val;
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci	val = REG_READ(chan->reg);
308c2ecf20Sopenharmony_ci	return (val & GPIO_CLOCK_VAL_IN) != 0;
318c2ecf20Sopenharmony_ci}
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_cistatic int get_data(void *data)
348c2ecf20Sopenharmony_ci{
358c2ecf20Sopenharmony_ci	struct psb_intel_i2c_chan *chan = data;
368c2ecf20Sopenharmony_ci	struct drm_device *dev = chan->drm_dev;
378c2ecf20Sopenharmony_ci	u32 val;
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci	val = REG_READ(chan->reg);
408c2ecf20Sopenharmony_ci	return (val & GPIO_DATA_VAL_IN) != 0;
418c2ecf20Sopenharmony_ci}
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic void set_clock(void *data, int state_high)
448c2ecf20Sopenharmony_ci{
458c2ecf20Sopenharmony_ci	struct psb_intel_i2c_chan *chan = data;
468c2ecf20Sopenharmony_ci	struct drm_device *dev = chan->drm_dev;
478c2ecf20Sopenharmony_ci	u32 reserved = 0, clock_bits;
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	/* On most chips, these bits must be preserved in software. */
508c2ecf20Sopenharmony_ci	reserved =
518c2ecf20Sopenharmony_ci		    REG_READ(chan->reg) & (GPIO_DATA_PULLUP_DISABLE |
528c2ecf20Sopenharmony_ci					   GPIO_CLOCK_PULLUP_DISABLE);
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	if (state_high)
558c2ecf20Sopenharmony_ci		clock_bits = GPIO_CLOCK_DIR_IN | GPIO_CLOCK_DIR_MASK;
568c2ecf20Sopenharmony_ci	else
578c2ecf20Sopenharmony_ci		clock_bits = GPIO_CLOCK_DIR_OUT | GPIO_CLOCK_DIR_MASK |
588c2ecf20Sopenharmony_ci		    GPIO_CLOCK_VAL_MASK;
598c2ecf20Sopenharmony_ci	REG_WRITE(chan->reg, reserved | clock_bits);
608c2ecf20Sopenharmony_ci	udelay(I2C_RISEFALL_TIME);	/* wait for the line to change state */
618c2ecf20Sopenharmony_ci}
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistatic void set_data(void *data, int state_high)
648c2ecf20Sopenharmony_ci{
658c2ecf20Sopenharmony_ci	struct psb_intel_i2c_chan *chan = data;
668c2ecf20Sopenharmony_ci	struct drm_device *dev = chan->drm_dev;
678c2ecf20Sopenharmony_ci	u32 reserved = 0, data_bits;
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	/* On most chips, these bits must be preserved in software. */
708c2ecf20Sopenharmony_ci	reserved =
718c2ecf20Sopenharmony_ci		    REG_READ(chan->reg) & (GPIO_DATA_PULLUP_DISABLE |
728c2ecf20Sopenharmony_ci					   GPIO_CLOCK_PULLUP_DISABLE);
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	if (state_high)
758c2ecf20Sopenharmony_ci		data_bits = GPIO_DATA_DIR_IN | GPIO_DATA_DIR_MASK;
768c2ecf20Sopenharmony_ci	else
778c2ecf20Sopenharmony_ci		data_bits =
788c2ecf20Sopenharmony_ci		    GPIO_DATA_DIR_OUT | GPIO_DATA_DIR_MASK |
798c2ecf20Sopenharmony_ci		    GPIO_DATA_VAL_MASK;
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	REG_WRITE(chan->reg, reserved | data_bits);
828c2ecf20Sopenharmony_ci	udelay(I2C_RISEFALL_TIME);	/* wait for the line to change state */
838c2ecf20Sopenharmony_ci}
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci/**
868c2ecf20Sopenharmony_ci * psb_intel_i2c_create - instantiate an Intel i2c bus using the specified GPIO reg
878c2ecf20Sopenharmony_ci * @dev: DRM device
888c2ecf20Sopenharmony_ci * @output: driver specific output device
898c2ecf20Sopenharmony_ci * @reg: GPIO reg to use
908c2ecf20Sopenharmony_ci * @name: name for this bus
918c2ecf20Sopenharmony_ci *
928c2ecf20Sopenharmony_ci * Creates and registers a new i2c bus with the Linux i2c layer, for use
938c2ecf20Sopenharmony_ci * in output probing and control (e.g. DDC or SDVO control functions).
948c2ecf20Sopenharmony_ci *
958c2ecf20Sopenharmony_ci * Possible values for @reg include:
968c2ecf20Sopenharmony_ci *   %GPIOA
978c2ecf20Sopenharmony_ci *   %GPIOB
988c2ecf20Sopenharmony_ci *   %GPIOC
998c2ecf20Sopenharmony_ci *   %GPIOD
1008c2ecf20Sopenharmony_ci *   %GPIOE
1018c2ecf20Sopenharmony_ci *   %GPIOF
1028c2ecf20Sopenharmony_ci *   %GPIOG
1038c2ecf20Sopenharmony_ci *   %GPIOH
1048c2ecf20Sopenharmony_ci * see PRM for details on how these different busses are used.
1058c2ecf20Sopenharmony_ci */
1068c2ecf20Sopenharmony_cistruct psb_intel_i2c_chan *psb_intel_i2c_create(struct drm_device *dev,
1078c2ecf20Sopenharmony_ci					const u32 reg, const char *name)
1088c2ecf20Sopenharmony_ci{
1098c2ecf20Sopenharmony_ci	struct psb_intel_i2c_chan *chan;
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	chan = kzalloc(sizeof(struct psb_intel_i2c_chan), GFP_KERNEL);
1128c2ecf20Sopenharmony_ci	if (!chan)
1138c2ecf20Sopenharmony_ci		goto out_free;
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	chan->drm_dev = dev;
1168c2ecf20Sopenharmony_ci	chan->reg = reg;
1178c2ecf20Sopenharmony_ci	snprintf(chan->adapter.name, I2C_NAME_SIZE, "intel drm %s", name);
1188c2ecf20Sopenharmony_ci	chan->adapter.owner = THIS_MODULE;
1198c2ecf20Sopenharmony_ci	chan->adapter.algo_data = &chan->algo;
1208c2ecf20Sopenharmony_ci	chan->adapter.dev.parent = &dev->pdev->dev;
1218c2ecf20Sopenharmony_ci	chan->algo.setsda = set_data;
1228c2ecf20Sopenharmony_ci	chan->algo.setscl = set_clock;
1238c2ecf20Sopenharmony_ci	chan->algo.getsda = get_data;
1248c2ecf20Sopenharmony_ci	chan->algo.getscl = get_clock;
1258c2ecf20Sopenharmony_ci	chan->algo.udelay = 20;
1268c2ecf20Sopenharmony_ci	chan->algo.timeout = usecs_to_jiffies(2200);
1278c2ecf20Sopenharmony_ci	chan->algo.data = chan;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	i2c_set_adapdata(&chan->adapter, chan);
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	if (i2c_bit_add_bus(&chan->adapter))
1328c2ecf20Sopenharmony_ci		goto out_free;
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	/* JJJ:  raise SCL and SDA? */
1358c2ecf20Sopenharmony_ci	set_data(chan, 1);
1368c2ecf20Sopenharmony_ci	set_clock(chan, 1);
1378c2ecf20Sopenharmony_ci	udelay(20);
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	return chan;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ciout_free:
1428c2ecf20Sopenharmony_ci	kfree(chan);
1438c2ecf20Sopenharmony_ci	return NULL;
1448c2ecf20Sopenharmony_ci}
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci/**
1478c2ecf20Sopenharmony_ci * psb_intel_i2c_destroy - unregister and free i2c bus resources
1488c2ecf20Sopenharmony_ci * @output: channel to free
1498c2ecf20Sopenharmony_ci *
1508c2ecf20Sopenharmony_ci * Unregister the adapter from the i2c layer, then free the structure.
1518c2ecf20Sopenharmony_ci */
1528c2ecf20Sopenharmony_civoid psb_intel_i2c_destroy(struct psb_intel_i2c_chan *chan)
1538c2ecf20Sopenharmony_ci{
1548c2ecf20Sopenharmony_ci	if (!chan)
1558c2ecf20Sopenharmony_ci		return;
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	i2c_del_adapter(&chan->adapter);
1588c2ecf20Sopenharmony_ci	kfree(chan);
1598c2ecf20Sopenharmony_ci}
160