162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright 2012 Red Hat Inc.
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
562306a36Sopenharmony_ci * copy of this software and associated documentation files (the
662306a36Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
762306a36Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
862306a36Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
962306a36Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
1062306a36Sopenharmony_ci * the following conditions:
1162306a36Sopenharmony_ci *
1262306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1362306a36Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1462306a36Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
1562306a36Sopenharmony_ci * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
1662306a36Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
1762306a36Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
1862306a36Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE.
1962306a36Sopenharmony_ci *
2062306a36Sopenharmony_ci * The above copyright notice and this permission notice (including the
2162306a36Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
2262306a36Sopenharmony_ci * of the Software.
2362306a36Sopenharmony_ci *
2462306a36Sopenharmony_ci */
2562306a36Sopenharmony_ci/*
2662306a36Sopenharmony_ci * Authors: Dave Airlie <airlied@redhat.com>
2762306a36Sopenharmony_ci */
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci#include <linux/export.h>
3062306a36Sopenharmony_ci#include <linux/i2c-algo-bit.h>
3162306a36Sopenharmony_ci#include <linux/i2c.h>
3262306a36Sopenharmony_ci#include <linux/pci.h>
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci#include "mgag200_drv.h"
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_cistatic int mga_i2c_read_gpio(struct mga_device *mdev)
3762306a36Sopenharmony_ci{
3862306a36Sopenharmony_ci	WREG8(DAC_INDEX, MGA1064_GEN_IO_DATA);
3962306a36Sopenharmony_ci	return RREG8(DAC_DATA);
4062306a36Sopenharmony_ci}
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_cistatic void mga_i2c_set_gpio(struct mga_device *mdev, int mask, int val)
4362306a36Sopenharmony_ci{
4462306a36Sopenharmony_ci	int tmp;
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci	WREG8(DAC_INDEX, MGA1064_GEN_IO_CTL);
4762306a36Sopenharmony_ci	tmp = (RREG8(DAC_DATA) & mask) | val;
4862306a36Sopenharmony_ci	WREG_DAC(MGA1064_GEN_IO_CTL, tmp);
4962306a36Sopenharmony_ci	WREG_DAC(MGA1064_GEN_IO_DATA, 0);
5062306a36Sopenharmony_ci}
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_cistatic inline void mga_i2c_set(struct mga_device *mdev, int mask, int state)
5362306a36Sopenharmony_ci{
5462306a36Sopenharmony_ci	if (state)
5562306a36Sopenharmony_ci		state = 0;
5662306a36Sopenharmony_ci	else
5762306a36Sopenharmony_ci		state = mask;
5862306a36Sopenharmony_ci	mga_i2c_set_gpio(mdev, ~mask, state);
5962306a36Sopenharmony_ci}
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_cistatic void mga_gpio_setsda(void *data, int state)
6262306a36Sopenharmony_ci{
6362306a36Sopenharmony_ci	struct mga_i2c_chan *i2c = data;
6462306a36Sopenharmony_ci	struct mga_device *mdev = to_mga_device(i2c->dev);
6562306a36Sopenharmony_ci	mga_i2c_set(mdev, i2c->data, state);
6662306a36Sopenharmony_ci}
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_cistatic void mga_gpio_setscl(void *data, int state)
6962306a36Sopenharmony_ci{
7062306a36Sopenharmony_ci	struct mga_i2c_chan *i2c = data;
7162306a36Sopenharmony_ci	struct mga_device *mdev = to_mga_device(i2c->dev);
7262306a36Sopenharmony_ci	mga_i2c_set(mdev, i2c->clock, state);
7362306a36Sopenharmony_ci}
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_cistatic int mga_gpio_getsda(void *data)
7662306a36Sopenharmony_ci{
7762306a36Sopenharmony_ci	struct mga_i2c_chan *i2c = data;
7862306a36Sopenharmony_ci	struct mga_device *mdev = to_mga_device(i2c->dev);
7962306a36Sopenharmony_ci	return (mga_i2c_read_gpio(mdev) & i2c->data) ? 1 : 0;
8062306a36Sopenharmony_ci}
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_cistatic int mga_gpio_getscl(void *data)
8362306a36Sopenharmony_ci{
8462306a36Sopenharmony_ci	struct mga_i2c_chan *i2c = data;
8562306a36Sopenharmony_ci	struct mga_device *mdev = to_mga_device(i2c->dev);
8662306a36Sopenharmony_ci	return (mga_i2c_read_gpio(mdev) & i2c->clock) ? 1 : 0;
8762306a36Sopenharmony_ci}
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_cistatic void mgag200_i2c_release(void *res)
9062306a36Sopenharmony_ci{
9162306a36Sopenharmony_ci	struct mga_i2c_chan *i2c = res;
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci	i2c_del_adapter(&i2c->adapter);
9462306a36Sopenharmony_ci}
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ciint mgag200_i2c_init(struct mga_device *mdev, struct mga_i2c_chan *i2c)
9762306a36Sopenharmony_ci{
9862306a36Sopenharmony_ci	struct drm_device *dev = &mdev->base;
9962306a36Sopenharmony_ci	const struct mgag200_device_info *info = mdev->info;
10062306a36Sopenharmony_ci	int ret;
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ci	WREG_DAC(MGA1064_GEN_IO_CTL2, 1);
10362306a36Sopenharmony_ci	WREG_DAC(MGA1064_GEN_IO_DATA, 0xff);
10462306a36Sopenharmony_ci	WREG_DAC(MGA1064_GEN_IO_CTL, 0);
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci	i2c->data = BIT(info->i2c.data_bit);
10762306a36Sopenharmony_ci	i2c->clock = BIT(info->i2c.clock_bit);
10862306a36Sopenharmony_ci	i2c->adapter.owner = THIS_MODULE;
10962306a36Sopenharmony_ci	i2c->adapter.class = I2C_CLASS_DDC;
11062306a36Sopenharmony_ci	i2c->adapter.dev.parent = dev->dev;
11162306a36Sopenharmony_ci	i2c->dev = dev;
11262306a36Sopenharmony_ci	i2c_set_adapdata(&i2c->adapter, i2c);
11362306a36Sopenharmony_ci	snprintf(i2c->adapter.name, sizeof(i2c->adapter.name), "mga i2c");
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci	i2c->adapter.algo_data = &i2c->bit;
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci	i2c->bit.udelay = 10;
11862306a36Sopenharmony_ci	i2c->bit.timeout = 2;
11962306a36Sopenharmony_ci	i2c->bit.data = i2c;
12062306a36Sopenharmony_ci	i2c->bit.setsda		= mga_gpio_setsda;
12162306a36Sopenharmony_ci	i2c->bit.setscl		= mga_gpio_setscl;
12262306a36Sopenharmony_ci	i2c->bit.getsda		= mga_gpio_getsda;
12362306a36Sopenharmony_ci	i2c->bit.getscl		= mga_gpio_getscl;
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci	ret = i2c_bit_add_bus(&i2c->adapter);
12662306a36Sopenharmony_ci	if (ret)
12762306a36Sopenharmony_ci		return ret;
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci	return devm_add_action_or_reset(dev->dev, mgag200_i2c_release, i2c);
13062306a36Sopenharmony_ci}
131