18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2007-2012 ST-Ericsson AB
48c2ecf20Sopenharmony_ci * ST DDC I2C master mode driver, used in e.g. U300 series platforms.
58c2ecf20Sopenharmony_ci * Author: Linus Walleij <linus.walleij@stericsson.com>
68c2ecf20Sopenharmony_ci * Author: Jonas Aaberg <jonas.aberg@stericsson.com>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci#include <linux/init.h>
98c2ecf20Sopenharmony_ci#include <linux/module.h>
108c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
118c2ecf20Sopenharmony_ci#include <linux/delay.h>
128c2ecf20Sopenharmony_ci#include <linux/i2c.h>
138c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
148c2ecf20Sopenharmony_ci#include <linux/completion.h>
158c2ecf20Sopenharmony_ci#include <linux/err.h>
168c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
178c2ecf20Sopenharmony_ci#include <linux/clk.h>
188c2ecf20Sopenharmony_ci#include <linux/io.h>
198c2ecf20Sopenharmony_ci#include <linux/slab.h>
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci/* the name of this kernel module */
228c2ecf20Sopenharmony_ci#define NAME "stu300"
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci/* CR (Control Register) 8bit (R/W) */
258c2ecf20Sopenharmony_ci#define I2C_CR					(0x00000000)
268c2ecf20Sopenharmony_ci#define I2C_CR_RESET_VALUE			(0x00)
278c2ecf20Sopenharmony_ci#define I2C_CR_RESET_UMASK			(0x00)
288c2ecf20Sopenharmony_ci#define I2C_CR_DDC1_ENABLE			(0x80)
298c2ecf20Sopenharmony_ci#define I2C_CR_TRANS_ENABLE			(0x40)
308c2ecf20Sopenharmony_ci#define I2C_CR_PERIPHERAL_ENABLE		(0x20)
318c2ecf20Sopenharmony_ci#define I2C_CR_DDC2B_ENABLE			(0x10)
328c2ecf20Sopenharmony_ci#define I2C_CR_START_ENABLE			(0x08)
338c2ecf20Sopenharmony_ci#define I2C_CR_ACK_ENABLE			(0x04)
348c2ecf20Sopenharmony_ci#define I2C_CR_STOP_ENABLE			(0x02)
358c2ecf20Sopenharmony_ci#define I2C_CR_INTERRUPT_ENABLE			(0x01)
368c2ecf20Sopenharmony_ci/* SR1 (Status Register 1) 8bit (R/-) */
378c2ecf20Sopenharmony_ci#define I2C_SR1					(0x00000004)
388c2ecf20Sopenharmony_ci#define I2C_SR1_RESET_VALUE			(0x00)
398c2ecf20Sopenharmony_ci#define I2C_SR1_RESET_UMASK			(0x00)
408c2ecf20Sopenharmony_ci#define I2C_SR1_EVF_IND				(0x80)
418c2ecf20Sopenharmony_ci#define I2C_SR1_ADD10_IND			(0x40)
428c2ecf20Sopenharmony_ci#define I2C_SR1_TRA_IND				(0x20)
438c2ecf20Sopenharmony_ci#define I2C_SR1_BUSY_IND			(0x10)
448c2ecf20Sopenharmony_ci#define I2C_SR1_BTF_IND				(0x08)
458c2ecf20Sopenharmony_ci#define I2C_SR1_ADSL_IND			(0x04)
468c2ecf20Sopenharmony_ci#define I2C_SR1_MSL_IND				(0x02)
478c2ecf20Sopenharmony_ci#define I2C_SR1_SB_IND				(0x01)
488c2ecf20Sopenharmony_ci/* SR2 (Status Register 2) 8bit (R/-) */
498c2ecf20Sopenharmony_ci#define I2C_SR2					(0x00000008)
508c2ecf20Sopenharmony_ci#define I2C_SR2_RESET_VALUE			(0x00)
518c2ecf20Sopenharmony_ci#define I2C_SR2_RESET_UMASK			(0x40)
528c2ecf20Sopenharmony_ci#define I2C_SR2_MASK				(0xBF)
538c2ecf20Sopenharmony_ci#define I2C_SR2_SCLFAL_IND			(0x80)
548c2ecf20Sopenharmony_ci#define I2C_SR2_ENDAD_IND			(0x20)
558c2ecf20Sopenharmony_ci#define I2C_SR2_AF_IND				(0x10)
568c2ecf20Sopenharmony_ci#define I2C_SR2_STOPF_IND			(0x08)
578c2ecf20Sopenharmony_ci#define I2C_SR2_ARLO_IND			(0x04)
588c2ecf20Sopenharmony_ci#define I2C_SR2_BERR_IND			(0x02)
598c2ecf20Sopenharmony_ci#define I2C_SR2_DDC2BF_IND			(0x01)
608c2ecf20Sopenharmony_ci/* CCR (Clock Control Register) 8bit (R/W) */
618c2ecf20Sopenharmony_ci#define I2C_CCR					(0x0000000C)
628c2ecf20Sopenharmony_ci#define I2C_CCR_RESET_VALUE			(0x00)
638c2ecf20Sopenharmony_ci#define I2C_CCR_RESET_UMASK			(0x00)
648c2ecf20Sopenharmony_ci#define I2C_CCR_MASK				(0xFF)
658c2ecf20Sopenharmony_ci#define I2C_CCR_FMSM				(0x80)
668c2ecf20Sopenharmony_ci#define I2C_CCR_CC_MASK				(0x7F)
678c2ecf20Sopenharmony_ci/* OAR1 (Own Address Register 1) 8bit (R/W) */
688c2ecf20Sopenharmony_ci#define I2C_OAR1				(0x00000010)
698c2ecf20Sopenharmony_ci#define I2C_OAR1_RESET_VALUE			(0x00)
708c2ecf20Sopenharmony_ci#define I2C_OAR1_RESET_UMASK			(0x00)
718c2ecf20Sopenharmony_ci#define I2C_OAR1_ADD_MASK			(0xFF)
728c2ecf20Sopenharmony_ci/* OAR2 (Own Address Register 2) 8bit (R/W) */
738c2ecf20Sopenharmony_ci#define I2C_OAR2				(0x00000014)
748c2ecf20Sopenharmony_ci#define I2C_OAR2_RESET_VALUE			(0x40)
758c2ecf20Sopenharmony_ci#define I2C_OAR2_RESET_UMASK			(0x19)
768c2ecf20Sopenharmony_ci#define I2C_OAR2_MASK				(0xE6)
778c2ecf20Sopenharmony_ci#define I2C_OAR2_FR_25_10MHZ			(0x00)
788c2ecf20Sopenharmony_ci#define I2C_OAR2_FR_10_1667MHZ			(0x20)
798c2ecf20Sopenharmony_ci#define I2C_OAR2_FR_1667_2667MHZ		(0x40)
808c2ecf20Sopenharmony_ci#define I2C_OAR2_FR_2667_40MHZ			(0x60)
818c2ecf20Sopenharmony_ci#define I2C_OAR2_FR_40_5333MHZ			(0x80)
828c2ecf20Sopenharmony_ci#define I2C_OAR2_FR_5333_66MHZ			(0xA0)
838c2ecf20Sopenharmony_ci#define I2C_OAR2_FR_66_80MHZ			(0xC0)
848c2ecf20Sopenharmony_ci#define I2C_OAR2_FR_80_100MHZ			(0xE0)
858c2ecf20Sopenharmony_ci#define I2C_OAR2_FR_MASK			(0xE0)
868c2ecf20Sopenharmony_ci#define I2C_OAR2_ADD_MASK			(0x06)
878c2ecf20Sopenharmony_ci/* DR (Data Register) 8bit (R/W) */
888c2ecf20Sopenharmony_ci#define I2C_DR					(0x00000018)
898c2ecf20Sopenharmony_ci#define I2C_DR_RESET_VALUE			(0x00)
908c2ecf20Sopenharmony_ci#define I2C_DR_RESET_UMASK			(0xFF)
918c2ecf20Sopenharmony_ci#define I2C_DR_D_MASK				(0xFF)
928c2ecf20Sopenharmony_ci/* ECCR (Extended Clock Control Register) 8bit (R/W) */
938c2ecf20Sopenharmony_ci#define I2C_ECCR				(0x0000001C)
948c2ecf20Sopenharmony_ci#define I2C_ECCR_RESET_VALUE			(0x00)
958c2ecf20Sopenharmony_ci#define I2C_ECCR_RESET_UMASK			(0xE0)
968c2ecf20Sopenharmony_ci#define I2C_ECCR_MASK				(0x1F)
978c2ecf20Sopenharmony_ci#define I2C_ECCR_CC_MASK			(0x1F)
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci/*
1008c2ecf20Sopenharmony_ci * These events are more or less responses to commands
1018c2ecf20Sopenharmony_ci * sent into the hardware, presumably reflecting the state
1028c2ecf20Sopenharmony_ci * of an internal state machine.
1038c2ecf20Sopenharmony_ci */
1048c2ecf20Sopenharmony_cienum stu300_event {
1058c2ecf20Sopenharmony_ci	STU300_EVENT_NONE = 0,
1068c2ecf20Sopenharmony_ci	STU300_EVENT_1,
1078c2ecf20Sopenharmony_ci	STU300_EVENT_2,
1088c2ecf20Sopenharmony_ci	STU300_EVENT_3,
1098c2ecf20Sopenharmony_ci	STU300_EVENT_4,
1108c2ecf20Sopenharmony_ci	STU300_EVENT_5,
1118c2ecf20Sopenharmony_ci	STU300_EVENT_6,
1128c2ecf20Sopenharmony_ci	STU300_EVENT_7,
1138c2ecf20Sopenharmony_ci	STU300_EVENT_8,
1148c2ecf20Sopenharmony_ci	STU300_EVENT_9
1158c2ecf20Sopenharmony_ci};
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_cienum stu300_error {
1188c2ecf20Sopenharmony_ci	STU300_ERROR_NONE = 0,
1198c2ecf20Sopenharmony_ci	STU300_ERROR_ACKNOWLEDGE_FAILURE,
1208c2ecf20Sopenharmony_ci	STU300_ERROR_BUS_ERROR,
1218c2ecf20Sopenharmony_ci	STU300_ERROR_ARBITRATION_LOST,
1228c2ecf20Sopenharmony_ci	STU300_ERROR_UNKNOWN
1238c2ecf20Sopenharmony_ci};
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci/* timeout waiting for the controller to respond */
1268c2ecf20Sopenharmony_ci#define STU300_TIMEOUT (msecs_to_jiffies(1000))
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci/*
1298c2ecf20Sopenharmony_ci * The number of address send athemps tried before giving up.
1308c2ecf20Sopenharmony_ci * If the first one fails it seems like 5 to 8 attempts are required.
1318c2ecf20Sopenharmony_ci */
1328c2ecf20Sopenharmony_ci#define NUM_ADDR_RESEND_ATTEMPTS 12
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci/* I2C clock speed, in Hz 0-400kHz*/
1358c2ecf20Sopenharmony_cistatic unsigned int scl_frequency = I2C_MAX_STANDARD_MODE_FREQ;
1368c2ecf20Sopenharmony_cimodule_param(scl_frequency, uint,  0644);
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci/**
1398c2ecf20Sopenharmony_ci * struct stu300_dev - the stu300 driver state holder
1408c2ecf20Sopenharmony_ci * @pdev: parent platform device
1418c2ecf20Sopenharmony_ci * @adapter: corresponding I2C adapter
1428c2ecf20Sopenharmony_ci * @clk: hardware block clock
1438c2ecf20Sopenharmony_ci * @irq: assigned interrupt line
1448c2ecf20Sopenharmony_ci * @cmd_issue_lock: this locks the following cmd_ variables
1458c2ecf20Sopenharmony_ci * @cmd_complete: acknowledge completion for an I2C command
1468c2ecf20Sopenharmony_ci * @cmd_event: expected event coming in as a response to a command
1478c2ecf20Sopenharmony_ci * @cmd_err: error code as response to a command
1488c2ecf20Sopenharmony_ci * @speed: current bus speed in Hz
1498c2ecf20Sopenharmony_ci * @msg_index: index of current message
1508c2ecf20Sopenharmony_ci * @msg_len: length of current message
1518c2ecf20Sopenharmony_ci */
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_cistruct stu300_dev {
1548c2ecf20Sopenharmony_ci	struct platform_device	*pdev;
1558c2ecf20Sopenharmony_ci	struct i2c_adapter	adapter;
1568c2ecf20Sopenharmony_ci	void __iomem		*virtbase;
1578c2ecf20Sopenharmony_ci	struct clk		*clk;
1588c2ecf20Sopenharmony_ci	int			irq;
1598c2ecf20Sopenharmony_ci	spinlock_t		cmd_issue_lock;
1608c2ecf20Sopenharmony_ci	struct completion	cmd_complete;
1618c2ecf20Sopenharmony_ci	enum stu300_event	cmd_event;
1628c2ecf20Sopenharmony_ci	enum stu300_error	cmd_err;
1638c2ecf20Sopenharmony_ci	unsigned int		speed;
1648c2ecf20Sopenharmony_ci	int			msg_index;
1658c2ecf20Sopenharmony_ci	int			msg_len;
1668c2ecf20Sopenharmony_ci};
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci/* Local forward function declarations */
1698c2ecf20Sopenharmony_cistatic int stu300_init_hw(struct stu300_dev *dev);
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci/*
1728c2ecf20Sopenharmony_ci * The block needs writes in both MSW and LSW in order
1738c2ecf20Sopenharmony_ci * for all data lines to reach their destination.
1748c2ecf20Sopenharmony_ci */
1758c2ecf20Sopenharmony_cistatic inline void stu300_wr8(u32 value, void __iomem *address)
1768c2ecf20Sopenharmony_ci{
1778c2ecf20Sopenharmony_ci	writel((value << 16) | value, address);
1788c2ecf20Sopenharmony_ci}
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci/*
1818c2ecf20Sopenharmony_ci * This merely masks off the duplicates which appear
1828c2ecf20Sopenharmony_ci * in bytes 1-3. You _MUST_ use 32-bit bus access on this
1838c2ecf20Sopenharmony_ci * device, else it will not work.
1848c2ecf20Sopenharmony_ci */
1858c2ecf20Sopenharmony_cistatic inline u32 stu300_r8(void __iomem *address)
1868c2ecf20Sopenharmony_ci{
1878c2ecf20Sopenharmony_ci	return readl(address) & 0x000000FFU;
1888c2ecf20Sopenharmony_ci}
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_cistatic void stu300_irq_enable(struct stu300_dev *dev)
1918c2ecf20Sopenharmony_ci{
1928c2ecf20Sopenharmony_ci	u32 val;
1938c2ecf20Sopenharmony_ci	val = stu300_r8(dev->virtbase + I2C_CR);
1948c2ecf20Sopenharmony_ci	val |= I2C_CR_INTERRUPT_ENABLE;
1958c2ecf20Sopenharmony_ci	/* Twice paranoia (possible HW glitch) */
1968c2ecf20Sopenharmony_ci	stu300_wr8(val, dev->virtbase + I2C_CR);
1978c2ecf20Sopenharmony_ci	stu300_wr8(val, dev->virtbase + I2C_CR);
1988c2ecf20Sopenharmony_ci}
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_cistatic void stu300_irq_disable(struct stu300_dev *dev)
2018c2ecf20Sopenharmony_ci{
2028c2ecf20Sopenharmony_ci	u32 val;
2038c2ecf20Sopenharmony_ci	val = stu300_r8(dev->virtbase + I2C_CR);
2048c2ecf20Sopenharmony_ci	val &= ~I2C_CR_INTERRUPT_ENABLE;
2058c2ecf20Sopenharmony_ci	/* Twice paranoia (possible HW glitch) */
2068c2ecf20Sopenharmony_ci	stu300_wr8(val, dev->virtbase + I2C_CR);
2078c2ecf20Sopenharmony_ci	stu300_wr8(val, dev->virtbase + I2C_CR);
2088c2ecf20Sopenharmony_ci}
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci/*
2128c2ecf20Sopenharmony_ci * Tells whether a certain event or events occurred in
2138c2ecf20Sopenharmony_ci * response to a command. The events represent states in
2148c2ecf20Sopenharmony_ci * the internal state machine of the hardware. The events
2158c2ecf20Sopenharmony_ci * are not very well described in the hardware
2168c2ecf20Sopenharmony_ci * documentation and can only be treated as abstract state
2178c2ecf20Sopenharmony_ci * machine states.
2188c2ecf20Sopenharmony_ci *
2198c2ecf20Sopenharmony_ci * @ret 0 = event has not occurred or unknown error, any
2208c2ecf20Sopenharmony_ci * other value means the correct event occurred or an error.
2218c2ecf20Sopenharmony_ci */
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_cistatic int stu300_event_occurred(struct stu300_dev *dev,
2248c2ecf20Sopenharmony_ci				   enum stu300_event mr_event) {
2258c2ecf20Sopenharmony_ci	u32 status1;
2268c2ecf20Sopenharmony_ci	u32 status2;
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	/* What event happened? */
2298c2ecf20Sopenharmony_ci	status1 = stu300_r8(dev->virtbase + I2C_SR1);
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	if (!(status1 & I2C_SR1_EVF_IND))
2328c2ecf20Sopenharmony_ci		/* No event at all */
2338c2ecf20Sopenharmony_ci		return 0;
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	status2 = stu300_r8(dev->virtbase + I2C_SR2);
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	/* Block any multiple interrupts */
2388c2ecf20Sopenharmony_ci	stu300_irq_disable(dev);
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	/* Check for errors first */
2418c2ecf20Sopenharmony_ci	if (status2 & I2C_SR2_AF_IND) {
2428c2ecf20Sopenharmony_ci		dev->cmd_err = STU300_ERROR_ACKNOWLEDGE_FAILURE;
2438c2ecf20Sopenharmony_ci		return 1;
2448c2ecf20Sopenharmony_ci	} else if (status2 & I2C_SR2_BERR_IND) {
2458c2ecf20Sopenharmony_ci		dev->cmd_err = STU300_ERROR_BUS_ERROR;
2468c2ecf20Sopenharmony_ci		return 1;
2478c2ecf20Sopenharmony_ci	} else if (status2 & I2C_SR2_ARLO_IND) {
2488c2ecf20Sopenharmony_ci		dev->cmd_err = STU300_ERROR_ARBITRATION_LOST;
2498c2ecf20Sopenharmony_ci		return 1;
2508c2ecf20Sopenharmony_ci	}
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci	switch (mr_event) {
2538c2ecf20Sopenharmony_ci	case STU300_EVENT_1:
2548c2ecf20Sopenharmony_ci		if (status1 & I2C_SR1_ADSL_IND)
2558c2ecf20Sopenharmony_ci			return 1;
2568c2ecf20Sopenharmony_ci		break;
2578c2ecf20Sopenharmony_ci	case STU300_EVENT_2:
2588c2ecf20Sopenharmony_ci	case STU300_EVENT_3:
2598c2ecf20Sopenharmony_ci	case STU300_EVENT_7:
2608c2ecf20Sopenharmony_ci	case STU300_EVENT_8:
2618c2ecf20Sopenharmony_ci		if (status1 & I2C_SR1_BTF_IND) {
2628c2ecf20Sopenharmony_ci			return 1;
2638c2ecf20Sopenharmony_ci		}
2648c2ecf20Sopenharmony_ci		break;
2658c2ecf20Sopenharmony_ci	case STU300_EVENT_4:
2668c2ecf20Sopenharmony_ci		if (status2 & I2C_SR2_STOPF_IND)
2678c2ecf20Sopenharmony_ci			return 1;
2688c2ecf20Sopenharmony_ci		break;
2698c2ecf20Sopenharmony_ci	case STU300_EVENT_5:
2708c2ecf20Sopenharmony_ci		if (status1 & I2C_SR1_SB_IND)
2718c2ecf20Sopenharmony_ci			/* Clear start bit */
2728c2ecf20Sopenharmony_ci			return 1;
2738c2ecf20Sopenharmony_ci		break;
2748c2ecf20Sopenharmony_ci	case STU300_EVENT_6:
2758c2ecf20Sopenharmony_ci		if (status2 & I2C_SR2_ENDAD_IND) {
2768c2ecf20Sopenharmony_ci			/* First check for any errors */
2778c2ecf20Sopenharmony_ci			return 1;
2788c2ecf20Sopenharmony_ci		}
2798c2ecf20Sopenharmony_ci		break;
2808c2ecf20Sopenharmony_ci	case STU300_EVENT_9:
2818c2ecf20Sopenharmony_ci		if (status1 & I2C_SR1_ADD10_IND)
2828c2ecf20Sopenharmony_ci			return 1;
2838c2ecf20Sopenharmony_ci		break;
2848c2ecf20Sopenharmony_ci	default:
2858c2ecf20Sopenharmony_ci		break;
2868c2ecf20Sopenharmony_ci	}
2878c2ecf20Sopenharmony_ci	/* If we get here, we're on thin ice.
2888c2ecf20Sopenharmony_ci	 * Here we are in a status where we have
2898c2ecf20Sopenharmony_ci	 * gotten a response that does not match
2908c2ecf20Sopenharmony_ci	 * what we requested.
2918c2ecf20Sopenharmony_ci	 */
2928c2ecf20Sopenharmony_ci	dev->cmd_err = STU300_ERROR_UNKNOWN;
2938c2ecf20Sopenharmony_ci	dev_err(&dev->pdev->dev,
2948c2ecf20Sopenharmony_ci		"Unhandled interrupt! %d sr1: 0x%x sr2: 0x%x\n",
2958c2ecf20Sopenharmony_ci		mr_event, status1, status2);
2968c2ecf20Sopenharmony_ci	return 0;
2978c2ecf20Sopenharmony_ci}
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_cistatic irqreturn_t stu300_irh(int irq, void *data)
3008c2ecf20Sopenharmony_ci{
3018c2ecf20Sopenharmony_ci	struct stu300_dev *dev = data;
3028c2ecf20Sopenharmony_ci	int res;
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci	/* Just make sure that the block is clocked */
3058c2ecf20Sopenharmony_ci	clk_enable(dev->clk);
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	/* See if this was what we were waiting for */
3088c2ecf20Sopenharmony_ci	spin_lock(&dev->cmd_issue_lock);
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	res = stu300_event_occurred(dev, dev->cmd_event);
3118c2ecf20Sopenharmony_ci	if (res || dev->cmd_err != STU300_ERROR_NONE)
3128c2ecf20Sopenharmony_ci		complete(&dev->cmd_complete);
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	spin_unlock(&dev->cmd_issue_lock);
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci	clk_disable(dev->clk);
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
3198c2ecf20Sopenharmony_ci}
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci/*
3228c2ecf20Sopenharmony_ci * Sends a command and then waits for the bits masked by *flagmask*
3238c2ecf20Sopenharmony_ci * to go high or low by IRQ awaiting.
3248c2ecf20Sopenharmony_ci */
3258c2ecf20Sopenharmony_cistatic int stu300_start_and_await_event(struct stu300_dev *dev,
3268c2ecf20Sopenharmony_ci					  u8 cr_value,
3278c2ecf20Sopenharmony_ci					  enum stu300_event mr_event)
3288c2ecf20Sopenharmony_ci{
3298c2ecf20Sopenharmony_ci	int ret;
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci	/* Lock command issue, fill in an event we wait for */
3328c2ecf20Sopenharmony_ci	spin_lock_irq(&dev->cmd_issue_lock);
3338c2ecf20Sopenharmony_ci	init_completion(&dev->cmd_complete);
3348c2ecf20Sopenharmony_ci	dev->cmd_err = STU300_ERROR_NONE;
3358c2ecf20Sopenharmony_ci	dev->cmd_event = mr_event;
3368c2ecf20Sopenharmony_ci	spin_unlock_irq(&dev->cmd_issue_lock);
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci	/* Turn on interrupt, send command and wait. */
3398c2ecf20Sopenharmony_ci	cr_value |= I2C_CR_INTERRUPT_ENABLE;
3408c2ecf20Sopenharmony_ci	stu300_wr8(cr_value, dev->virtbase + I2C_CR);
3418c2ecf20Sopenharmony_ci	ret = wait_for_completion_interruptible_timeout(&dev->cmd_complete,
3428c2ecf20Sopenharmony_ci							STU300_TIMEOUT);
3438c2ecf20Sopenharmony_ci	if (ret < 0) {
3448c2ecf20Sopenharmony_ci		dev_err(&dev->pdev->dev,
3458c2ecf20Sopenharmony_ci		       "wait_for_completion_interruptible_timeout() "
3468c2ecf20Sopenharmony_ci		       "returned %d waiting for event %04x\n", ret, mr_event);
3478c2ecf20Sopenharmony_ci		return ret;
3488c2ecf20Sopenharmony_ci	}
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	if (ret == 0) {
3518c2ecf20Sopenharmony_ci		dev_err(&dev->pdev->dev, "controller timed out "
3528c2ecf20Sopenharmony_ci		       "waiting for event %d, reinit hardware\n", mr_event);
3538c2ecf20Sopenharmony_ci		(void) stu300_init_hw(dev);
3548c2ecf20Sopenharmony_ci		return -ETIMEDOUT;
3558c2ecf20Sopenharmony_ci	}
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	if (dev->cmd_err != STU300_ERROR_NONE) {
3588c2ecf20Sopenharmony_ci		dev_err(&dev->pdev->dev, "controller (start) "
3598c2ecf20Sopenharmony_ci		       "error %d waiting for event %d, reinit hardware\n",
3608c2ecf20Sopenharmony_ci		       dev->cmd_err, mr_event);
3618c2ecf20Sopenharmony_ci		(void) stu300_init_hw(dev);
3628c2ecf20Sopenharmony_ci		return -EIO;
3638c2ecf20Sopenharmony_ci	}
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci	return 0;
3668c2ecf20Sopenharmony_ci}
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci/*
3698c2ecf20Sopenharmony_ci * This waits for a flag to be set, if it is not set on entry, an interrupt is
3708c2ecf20Sopenharmony_ci * configured to wait for the flag using a completion.
3718c2ecf20Sopenharmony_ci */
3728c2ecf20Sopenharmony_cistatic int stu300_await_event(struct stu300_dev *dev,
3738c2ecf20Sopenharmony_ci				enum stu300_event mr_event)
3748c2ecf20Sopenharmony_ci{
3758c2ecf20Sopenharmony_ci	int ret;
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci	/* Is it already here? */
3788c2ecf20Sopenharmony_ci	spin_lock_irq(&dev->cmd_issue_lock);
3798c2ecf20Sopenharmony_ci	dev->cmd_err = STU300_ERROR_NONE;
3808c2ecf20Sopenharmony_ci	dev->cmd_event = mr_event;
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci	init_completion(&dev->cmd_complete);
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci	/* Turn on the I2C interrupt for current operation */
3858c2ecf20Sopenharmony_ci	stu300_irq_enable(dev);
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci	/* Unlock the command block and wait for the event to occur */
3888c2ecf20Sopenharmony_ci	spin_unlock_irq(&dev->cmd_issue_lock);
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci	ret = wait_for_completion_interruptible_timeout(&dev->cmd_complete,
3918c2ecf20Sopenharmony_ci							STU300_TIMEOUT);
3928c2ecf20Sopenharmony_ci	if (ret < 0) {
3938c2ecf20Sopenharmony_ci		dev_err(&dev->pdev->dev,
3948c2ecf20Sopenharmony_ci		       "wait_for_completion_interruptible_timeout()"
3958c2ecf20Sopenharmony_ci		       "returned %d waiting for event %04x\n", ret, mr_event);
3968c2ecf20Sopenharmony_ci		return ret;
3978c2ecf20Sopenharmony_ci	}
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci	if (ret == 0) {
4008c2ecf20Sopenharmony_ci		if (mr_event != STU300_EVENT_6) {
4018c2ecf20Sopenharmony_ci			dev_err(&dev->pdev->dev, "controller "
4028c2ecf20Sopenharmony_ci				"timed out waiting for event %d, reinit "
4038c2ecf20Sopenharmony_ci				"hardware\n", mr_event);
4048c2ecf20Sopenharmony_ci			(void) stu300_init_hw(dev);
4058c2ecf20Sopenharmony_ci		}
4068c2ecf20Sopenharmony_ci		return -ETIMEDOUT;
4078c2ecf20Sopenharmony_ci	}
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci	if (dev->cmd_err != STU300_ERROR_NONE) {
4108c2ecf20Sopenharmony_ci		if (mr_event != STU300_EVENT_6) {
4118c2ecf20Sopenharmony_ci			dev_err(&dev->pdev->dev, "controller "
4128c2ecf20Sopenharmony_ci				"error (await_event) %d waiting for event %d, "
4138c2ecf20Sopenharmony_ci			       "reinit hardware\n", dev->cmd_err, mr_event);
4148c2ecf20Sopenharmony_ci			(void) stu300_init_hw(dev);
4158c2ecf20Sopenharmony_ci		}
4168c2ecf20Sopenharmony_ci		return -EIO;
4178c2ecf20Sopenharmony_ci	}
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci	return 0;
4208c2ecf20Sopenharmony_ci}
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci/*
4238c2ecf20Sopenharmony_ci * Waits for the busy bit to go low by repeated polling.
4248c2ecf20Sopenharmony_ci */
4258c2ecf20Sopenharmony_ci#define BUSY_RELEASE_ATTEMPTS 10
4268c2ecf20Sopenharmony_cistatic int stu300_wait_while_busy(struct stu300_dev *dev)
4278c2ecf20Sopenharmony_ci{
4288c2ecf20Sopenharmony_ci	unsigned long timeout;
4298c2ecf20Sopenharmony_ci	int i;
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci	for (i = 0; i < BUSY_RELEASE_ATTEMPTS; i++) {
4328c2ecf20Sopenharmony_ci		timeout = jiffies + STU300_TIMEOUT;
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci		while (!time_after(jiffies, timeout)) {
4358c2ecf20Sopenharmony_ci			/* Is not busy? */
4368c2ecf20Sopenharmony_ci			if ((stu300_r8(dev->virtbase + I2C_SR1) &
4378c2ecf20Sopenharmony_ci			     I2C_SR1_BUSY_IND) == 0)
4388c2ecf20Sopenharmony_ci				return 0;
4398c2ecf20Sopenharmony_ci			msleep(1);
4408c2ecf20Sopenharmony_ci		}
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci		dev_err(&dev->pdev->dev, "transaction timed out "
4438c2ecf20Sopenharmony_ci			"waiting for device to be free (not busy). "
4448c2ecf20Sopenharmony_ci		       "Attempt: %d\n", i+1);
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci		dev_err(&dev->pdev->dev, "base address = "
4478c2ecf20Sopenharmony_ci			"0x%p, reinit hardware\n", dev->virtbase);
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci		(void) stu300_init_hw(dev);
4508c2ecf20Sopenharmony_ci	}
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci	dev_err(&dev->pdev->dev, "giving up after %d attempts "
4538c2ecf20Sopenharmony_ci		"to reset the bus.\n",  BUSY_RELEASE_ATTEMPTS);
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci	return -ETIMEDOUT;
4568c2ecf20Sopenharmony_ci}
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_cistruct stu300_clkset {
4598c2ecf20Sopenharmony_ci	unsigned long rate;
4608c2ecf20Sopenharmony_ci	u32 setting;
4618c2ecf20Sopenharmony_ci};
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_cistatic const struct stu300_clkset stu300_clktable[] = {
4648c2ecf20Sopenharmony_ci	{ 0,         0xFFU },
4658c2ecf20Sopenharmony_ci	{ 2500000,   I2C_OAR2_FR_25_10MHZ },
4668c2ecf20Sopenharmony_ci	{ 10000000,  I2C_OAR2_FR_10_1667MHZ },
4678c2ecf20Sopenharmony_ci	{ 16670000,  I2C_OAR2_FR_1667_2667MHZ },
4688c2ecf20Sopenharmony_ci	{ 26670000,  I2C_OAR2_FR_2667_40MHZ },
4698c2ecf20Sopenharmony_ci	{ 40000000,  I2C_OAR2_FR_40_5333MHZ },
4708c2ecf20Sopenharmony_ci	{ 53330000,  I2C_OAR2_FR_5333_66MHZ },
4718c2ecf20Sopenharmony_ci	{ 66000000,  I2C_OAR2_FR_66_80MHZ },
4728c2ecf20Sopenharmony_ci	{ 80000000,  I2C_OAR2_FR_80_100MHZ },
4738c2ecf20Sopenharmony_ci	{ 100000000, 0xFFU },
4748c2ecf20Sopenharmony_ci};
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_cistatic int stu300_set_clk(struct stu300_dev *dev, unsigned long clkrate)
4788c2ecf20Sopenharmony_ci{
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_ci	u32 val;
4818c2ecf20Sopenharmony_ci	int i = 0;
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci	/* Locate the appropriate clock setting */
4848c2ecf20Sopenharmony_ci	while (i < ARRAY_SIZE(stu300_clktable) - 1 &&
4858c2ecf20Sopenharmony_ci	       stu300_clktable[i].rate < clkrate)
4868c2ecf20Sopenharmony_ci		i++;
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_ci	if (stu300_clktable[i].setting == 0xFFU) {
4898c2ecf20Sopenharmony_ci		dev_err(&dev->pdev->dev, "too %s clock rate requested "
4908c2ecf20Sopenharmony_ci			"(%lu Hz).\n", i ? "high" : "low", clkrate);
4918c2ecf20Sopenharmony_ci		return -EINVAL;
4928c2ecf20Sopenharmony_ci	}
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci	stu300_wr8(stu300_clktable[i].setting,
4958c2ecf20Sopenharmony_ci		   dev->virtbase + I2C_OAR2);
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_ci	dev_dbg(&dev->pdev->dev, "Clock rate %lu Hz, I2C bus speed %d Hz "
4988c2ecf20Sopenharmony_ci		"virtbase %p\n", clkrate, dev->speed, dev->virtbase);
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci	if (dev->speed > I2C_MAX_STANDARD_MODE_FREQ)
5018c2ecf20Sopenharmony_ci		/* Fast Mode I2C */
5028c2ecf20Sopenharmony_ci		val = ((clkrate/dev->speed) - 9)/3 + 1;
5038c2ecf20Sopenharmony_ci	else
5048c2ecf20Sopenharmony_ci		/* Standard Mode I2C */
5058c2ecf20Sopenharmony_ci		val = ((clkrate/dev->speed) - 7)/2 + 1;
5068c2ecf20Sopenharmony_ci
5078c2ecf20Sopenharmony_ci	/* According to spec the divider must be > 2 */
5088c2ecf20Sopenharmony_ci	if (val < 0x002) {
5098c2ecf20Sopenharmony_ci		dev_err(&dev->pdev->dev, "too low clock rate (%lu Hz).\n",
5108c2ecf20Sopenharmony_ci			clkrate);
5118c2ecf20Sopenharmony_ci		return -EINVAL;
5128c2ecf20Sopenharmony_ci	}
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci	/* We have 12 bits clock divider only! */
5158c2ecf20Sopenharmony_ci	if (val & 0xFFFFF000U) {
5168c2ecf20Sopenharmony_ci		dev_err(&dev->pdev->dev, "too high clock rate (%lu Hz).\n",
5178c2ecf20Sopenharmony_ci			clkrate);
5188c2ecf20Sopenharmony_ci		return -EINVAL;
5198c2ecf20Sopenharmony_ci	}
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ci	if (dev->speed > I2C_MAX_STANDARD_MODE_FREQ) {
5228c2ecf20Sopenharmony_ci		/* CC6..CC0 */
5238c2ecf20Sopenharmony_ci		stu300_wr8((val & I2C_CCR_CC_MASK) | I2C_CCR_FMSM,
5248c2ecf20Sopenharmony_ci			   dev->virtbase + I2C_CCR);
5258c2ecf20Sopenharmony_ci		dev_dbg(&dev->pdev->dev, "set clock divider to 0x%08x, "
5268c2ecf20Sopenharmony_ci			"Fast Mode I2C\n", val);
5278c2ecf20Sopenharmony_ci	} else {
5288c2ecf20Sopenharmony_ci		/* CC6..CC0 */
5298c2ecf20Sopenharmony_ci		stu300_wr8((val & I2C_CCR_CC_MASK),
5308c2ecf20Sopenharmony_ci			   dev->virtbase + I2C_CCR);
5318c2ecf20Sopenharmony_ci		dev_dbg(&dev->pdev->dev, "set clock divider to "
5328c2ecf20Sopenharmony_ci			"0x%08x, Standard Mode I2C\n", val);
5338c2ecf20Sopenharmony_ci	}
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci	/* CC11..CC7 */
5368c2ecf20Sopenharmony_ci	stu300_wr8(((val >> 7) & 0x1F),
5378c2ecf20Sopenharmony_ci		   dev->virtbase + I2C_ECCR);
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_ci	return 0;
5408c2ecf20Sopenharmony_ci}
5418c2ecf20Sopenharmony_ci
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_cistatic int stu300_init_hw(struct stu300_dev *dev)
5448c2ecf20Sopenharmony_ci{
5458c2ecf20Sopenharmony_ci	u32 dummy;
5468c2ecf20Sopenharmony_ci	unsigned long clkrate;
5478c2ecf20Sopenharmony_ci	int ret;
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_ci	/* Disable controller */
5508c2ecf20Sopenharmony_ci	stu300_wr8(0x00, dev->virtbase + I2C_CR);
5518c2ecf20Sopenharmony_ci	/*
5528c2ecf20Sopenharmony_ci	 * Set own address to some default value (0x00).
5538c2ecf20Sopenharmony_ci	 * We do not support slave mode anyway.
5548c2ecf20Sopenharmony_ci	 */
5558c2ecf20Sopenharmony_ci	stu300_wr8(0x00, dev->virtbase + I2C_OAR1);
5568c2ecf20Sopenharmony_ci	/*
5578c2ecf20Sopenharmony_ci	 * The I2C controller only operates properly in 26 MHz but we
5588c2ecf20Sopenharmony_ci	 * program this driver as if we didn't know. This will also set the two
5598c2ecf20Sopenharmony_ci	 * high bits of the own address to zero as well.
5608c2ecf20Sopenharmony_ci	 * There is no known hardware issue with running in 13 MHz
5618c2ecf20Sopenharmony_ci	 * However, speeds over 200 kHz are not used.
5628c2ecf20Sopenharmony_ci	 */
5638c2ecf20Sopenharmony_ci	clkrate = clk_get_rate(dev->clk);
5648c2ecf20Sopenharmony_ci	ret = stu300_set_clk(dev, clkrate);
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_ci	if (ret)
5678c2ecf20Sopenharmony_ci		return ret;
5688c2ecf20Sopenharmony_ci	/*
5698c2ecf20Sopenharmony_ci	 * Enable block, do it TWICE (hardware glitch)
5708c2ecf20Sopenharmony_ci	 * Setting bit 7 can enable DDC mode. (Not used currently.)
5718c2ecf20Sopenharmony_ci	 */
5728c2ecf20Sopenharmony_ci	stu300_wr8(I2C_CR_PERIPHERAL_ENABLE,
5738c2ecf20Sopenharmony_ci				  dev->virtbase + I2C_CR);
5748c2ecf20Sopenharmony_ci	stu300_wr8(I2C_CR_PERIPHERAL_ENABLE,
5758c2ecf20Sopenharmony_ci				  dev->virtbase + I2C_CR);
5768c2ecf20Sopenharmony_ci	/* Make a dummy read of the status register SR1 & SR2 */
5778c2ecf20Sopenharmony_ci	dummy = stu300_r8(dev->virtbase + I2C_SR2);
5788c2ecf20Sopenharmony_ci	dummy = stu300_r8(dev->virtbase + I2C_SR1);
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_ci	return 0;
5818c2ecf20Sopenharmony_ci}
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_ci/* Send slave address. */
5868c2ecf20Sopenharmony_cistatic int stu300_send_address(struct stu300_dev *dev,
5878c2ecf20Sopenharmony_ci				 struct i2c_msg *msg, int resend)
5888c2ecf20Sopenharmony_ci{
5898c2ecf20Sopenharmony_ci	u32 val;
5908c2ecf20Sopenharmony_ci	int ret;
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_ci	if (msg->flags & I2C_M_TEN) {
5938c2ecf20Sopenharmony_ci		/* This is probably how 10 bit addresses look */
5948c2ecf20Sopenharmony_ci		val = (0xf0 | (((u32) msg->addr & 0x300) >> 7)) &
5958c2ecf20Sopenharmony_ci			I2C_DR_D_MASK;
5968c2ecf20Sopenharmony_ci		if (msg->flags & I2C_M_RD)
5978c2ecf20Sopenharmony_ci			/* This is the direction bit */
5988c2ecf20Sopenharmony_ci			val |= 0x01;
5998c2ecf20Sopenharmony_ci	} else {
6008c2ecf20Sopenharmony_ci		val = i2c_8bit_addr_from_msg(msg);
6018c2ecf20Sopenharmony_ci	}
6028c2ecf20Sopenharmony_ci
6038c2ecf20Sopenharmony_ci	if (resend) {
6048c2ecf20Sopenharmony_ci		if (msg->flags & I2C_M_RD)
6058c2ecf20Sopenharmony_ci			dev_dbg(&dev->pdev->dev, "read resend\n");
6068c2ecf20Sopenharmony_ci		else
6078c2ecf20Sopenharmony_ci			dev_dbg(&dev->pdev->dev, "write resend\n");
6088c2ecf20Sopenharmony_ci	}
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_ci	stu300_wr8(val, dev->virtbase + I2C_DR);
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_ci	/* For 10bit addressing, await 10bit request (EVENT 9) */
6138c2ecf20Sopenharmony_ci	if (msg->flags & I2C_M_TEN) {
6148c2ecf20Sopenharmony_ci		ret = stu300_await_event(dev, STU300_EVENT_9);
6158c2ecf20Sopenharmony_ci		/*
6168c2ecf20Sopenharmony_ci		 * The slave device wants a 10bit address, send the rest
6178c2ecf20Sopenharmony_ci		 * of the bits (the LSBits)
6188c2ecf20Sopenharmony_ci		 */
6198c2ecf20Sopenharmony_ci		val = msg->addr & I2C_DR_D_MASK;
6208c2ecf20Sopenharmony_ci		/* This clears "event 9" */
6218c2ecf20Sopenharmony_ci		stu300_wr8(val, dev->virtbase + I2C_DR);
6228c2ecf20Sopenharmony_ci		if (ret != 0)
6238c2ecf20Sopenharmony_ci			return ret;
6248c2ecf20Sopenharmony_ci	}
6258c2ecf20Sopenharmony_ci	/* FIXME: Why no else here? two events for 10bit?
6268c2ecf20Sopenharmony_ci	 * Await event 6 (normal) or event 9 (10bit)
6278c2ecf20Sopenharmony_ci	 */
6288c2ecf20Sopenharmony_ci
6298c2ecf20Sopenharmony_ci	if (resend)
6308c2ecf20Sopenharmony_ci		dev_dbg(&dev->pdev->dev, "await event 6\n");
6318c2ecf20Sopenharmony_ci	ret = stu300_await_event(dev, STU300_EVENT_6);
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_ci	/*
6348c2ecf20Sopenharmony_ci	 * Clear any pending EVENT 6 no matter what happened during
6358c2ecf20Sopenharmony_ci	 * await_event.
6368c2ecf20Sopenharmony_ci	 */
6378c2ecf20Sopenharmony_ci	val = stu300_r8(dev->virtbase + I2C_CR);
6388c2ecf20Sopenharmony_ci	val |= I2C_CR_PERIPHERAL_ENABLE;
6398c2ecf20Sopenharmony_ci	stu300_wr8(val, dev->virtbase + I2C_CR);
6408c2ecf20Sopenharmony_ci
6418c2ecf20Sopenharmony_ci	return ret;
6428c2ecf20Sopenharmony_ci}
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_cistatic int stu300_xfer_msg(struct i2c_adapter *adap,
6458c2ecf20Sopenharmony_ci			     struct i2c_msg *msg, int stop)
6468c2ecf20Sopenharmony_ci{
6478c2ecf20Sopenharmony_ci	u32 cr;
6488c2ecf20Sopenharmony_ci	u32 val;
6498c2ecf20Sopenharmony_ci	u32 i;
6508c2ecf20Sopenharmony_ci	int ret;
6518c2ecf20Sopenharmony_ci	int attempts = 0;
6528c2ecf20Sopenharmony_ci	struct stu300_dev *dev = i2c_get_adapdata(adap);
6538c2ecf20Sopenharmony_ci
6548c2ecf20Sopenharmony_ci	clk_enable(dev->clk);
6558c2ecf20Sopenharmony_ci
6568c2ecf20Sopenharmony_ci	/* Remove this if (0) to trace each and every message. */
6578c2ecf20Sopenharmony_ci	if (0) {
6588c2ecf20Sopenharmony_ci		dev_dbg(&dev->pdev->dev, "I2C message to: 0x%04x, len: %d, "
6598c2ecf20Sopenharmony_ci			"flags: 0x%04x, stop: %d\n",
6608c2ecf20Sopenharmony_ci			msg->addr, msg->len, msg->flags, stop);
6618c2ecf20Sopenharmony_ci	}
6628c2ecf20Sopenharmony_ci
6638c2ecf20Sopenharmony_ci	/*
6648c2ecf20Sopenharmony_ci	 * For some reason, sending the address sometimes fails when running
6658c2ecf20Sopenharmony_ci	 * on  the 13 MHz clock. No interrupt arrives. This is a work around,
6668c2ecf20Sopenharmony_ci	 * which tries to restart and send the address up to 10 times before
6678c2ecf20Sopenharmony_ci	 * really giving up. Usually 5 to 8 attempts are enough.
6688c2ecf20Sopenharmony_ci	 */
6698c2ecf20Sopenharmony_ci	do {
6708c2ecf20Sopenharmony_ci		if (attempts)
6718c2ecf20Sopenharmony_ci			dev_dbg(&dev->pdev->dev, "wait while busy\n");
6728c2ecf20Sopenharmony_ci		/* Check that the bus is free, or wait until some timeout */
6738c2ecf20Sopenharmony_ci		ret = stu300_wait_while_busy(dev);
6748c2ecf20Sopenharmony_ci		if (ret != 0)
6758c2ecf20Sopenharmony_ci			goto exit_disable;
6768c2ecf20Sopenharmony_ci
6778c2ecf20Sopenharmony_ci		if (attempts)
6788c2ecf20Sopenharmony_ci			dev_dbg(&dev->pdev->dev, "re-int hw\n");
6798c2ecf20Sopenharmony_ci		/*
6808c2ecf20Sopenharmony_ci		 * According to ST, there is no problem if the clock is
6818c2ecf20Sopenharmony_ci		 * changed between 13 and 26 MHz during a transfer.
6828c2ecf20Sopenharmony_ci		 */
6838c2ecf20Sopenharmony_ci		ret = stu300_init_hw(dev);
6848c2ecf20Sopenharmony_ci		if (ret)
6858c2ecf20Sopenharmony_ci			goto exit_disable;
6868c2ecf20Sopenharmony_ci
6878c2ecf20Sopenharmony_ci		/* Send a start condition */
6888c2ecf20Sopenharmony_ci		cr = I2C_CR_PERIPHERAL_ENABLE;
6898c2ecf20Sopenharmony_ci		/* Setting the START bit puts the block in master mode */
6908c2ecf20Sopenharmony_ci		if (!(msg->flags & I2C_M_NOSTART))
6918c2ecf20Sopenharmony_ci			cr |= I2C_CR_START_ENABLE;
6928c2ecf20Sopenharmony_ci		if ((msg->flags & I2C_M_RD) && (msg->len > 1))
6938c2ecf20Sopenharmony_ci			/* On read more than 1 byte, we need ack. */
6948c2ecf20Sopenharmony_ci			cr |= I2C_CR_ACK_ENABLE;
6958c2ecf20Sopenharmony_ci		/* Check that it gets through */
6968c2ecf20Sopenharmony_ci		if (!(msg->flags & I2C_M_NOSTART)) {
6978c2ecf20Sopenharmony_ci			if (attempts)
6988c2ecf20Sopenharmony_ci				dev_dbg(&dev->pdev->dev, "send start event\n");
6998c2ecf20Sopenharmony_ci			ret = stu300_start_and_await_event(dev, cr,
7008c2ecf20Sopenharmony_ci							     STU300_EVENT_5);
7018c2ecf20Sopenharmony_ci		}
7028c2ecf20Sopenharmony_ci
7038c2ecf20Sopenharmony_ci		if (attempts)
7048c2ecf20Sopenharmony_ci			dev_dbg(&dev->pdev->dev, "send address\n");
7058c2ecf20Sopenharmony_ci
7068c2ecf20Sopenharmony_ci		if (ret == 0)
7078c2ecf20Sopenharmony_ci			/* Send address */
7088c2ecf20Sopenharmony_ci			ret = stu300_send_address(dev, msg, attempts != 0);
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ci		if (ret != 0) {
7118c2ecf20Sopenharmony_ci			attempts++;
7128c2ecf20Sopenharmony_ci			dev_dbg(&dev->pdev->dev, "failed sending address, "
7138c2ecf20Sopenharmony_ci				"retrying. Attempt: %d msg_index: %d/%d\n",
7148c2ecf20Sopenharmony_ci			       attempts, dev->msg_index, dev->msg_len);
7158c2ecf20Sopenharmony_ci		}
7168c2ecf20Sopenharmony_ci
7178c2ecf20Sopenharmony_ci	} while (ret != 0 && attempts < NUM_ADDR_RESEND_ATTEMPTS);
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_ci	if (attempts < NUM_ADDR_RESEND_ATTEMPTS && attempts > 0) {
7208c2ecf20Sopenharmony_ci		dev_dbg(&dev->pdev->dev, "managed to get address "
7218c2ecf20Sopenharmony_ci			"through after %d attempts\n", attempts);
7228c2ecf20Sopenharmony_ci	} else if (attempts == NUM_ADDR_RESEND_ATTEMPTS) {
7238c2ecf20Sopenharmony_ci		dev_dbg(&dev->pdev->dev, "I give up, tried %d times "
7248c2ecf20Sopenharmony_ci			"to resend address.\n",
7258c2ecf20Sopenharmony_ci			NUM_ADDR_RESEND_ATTEMPTS);
7268c2ecf20Sopenharmony_ci		goto exit_disable;
7278c2ecf20Sopenharmony_ci	}
7288c2ecf20Sopenharmony_ci
7298c2ecf20Sopenharmony_ci
7308c2ecf20Sopenharmony_ci	if (msg->flags & I2C_M_RD) {
7318c2ecf20Sopenharmony_ci		/* READ: we read the actual bytes one at a time */
7328c2ecf20Sopenharmony_ci		for (i = 0; i < msg->len; i++) {
7338c2ecf20Sopenharmony_ci			if (i == msg->len-1) {
7348c2ecf20Sopenharmony_ci				/*
7358c2ecf20Sopenharmony_ci				 * Disable ACK and set STOP condition before
7368c2ecf20Sopenharmony_ci				 * reading last byte
7378c2ecf20Sopenharmony_ci				 */
7388c2ecf20Sopenharmony_ci				val = I2C_CR_PERIPHERAL_ENABLE;
7398c2ecf20Sopenharmony_ci
7408c2ecf20Sopenharmony_ci				if (stop)
7418c2ecf20Sopenharmony_ci					val |= I2C_CR_STOP_ENABLE;
7428c2ecf20Sopenharmony_ci
7438c2ecf20Sopenharmony_ci				stu300_wr8(val,
7448c2ecf20Sopenharmony_ci					   dev->virtbase + I2C_CR);
7458c2ecf20Sopenharmony_ci			}
7468c2ecf20Sopenharmony_ci			/* Wait for this byte... */
7478c2ecf20Sopenharmony_ci			ret = stu300_await_event(dev, STU300_EVENT_7);
7488c2ecf20Sopenharmony_ci			if (ret != 0)
7498c2ecf20Sopenharmony_ci				goto exit_disable;
7508c2ecf20Sopenharmony_ci			/* This clears event 7 */
7518c2ecf20Sopenharmony_ci			msg->buf[i] = (u8) stu300_r8(dev->virtbase + I2C_DR);
7528c2ecf20Sopenharmony_ci		}
7538c2ecf20Sopenharmony_ci	} else {
7548c2ecf20Sopenharmony_ci		/* WRITE: we send the actual bytes one at a time */
7558c2ecf20Sopenharmony_ci		for (i = 0; i < msg->len; i++) {
7568c2ecf20Sopenharmony_ci			/* Write the byte */
7578c2ecf20Sopenharmony_ci			stu300_wr8(msg->buf[i],
7588c2ecf20Sopenharmony_ci				   dev->virtbase + I2C_DR);
7598c2ecf20Sopenharmony_ci			/* Check status */
7608c2ecf20Sopenharmony_ci			ret = stu300_await_event(dev, STU300_EVENT_8);
7618c2ecf20Sopenharmony_ci			/* Next write to DR will clear event 8 */
7628c2ecf20Sopenharmony_ci			if (ret != 0) {
7638c2ecf20Sopenharmony_ci				dev_err(&dev->pdev->dev, "error awaiting "
7648c2ecf20Sopenharmony_ci				       "event 8 (%d)\n", ret);
7658c2ecf20Sopenharmony_ci				goto exit_disable;
7668c2ecf20Sopenharmony_ci			}
7678c2ecf20Sopenharmony_ci		}
7688c2ecf20Sopenharmony_ci		/* Check NAK */
7698c2ecf20Sopenharmony_ci		if (!(msg->flags & I2C_M_IGNORE_NAK)) {
7708c2ecf20Sopenharmony_ci			if (stu300_r8(dev->virtbase + I2C_SR2) &
7718c2ecf20Sopenharmony_ci			    I2C_SR2_AF_IND) {
7728c2ecf20Sopenharmony_ci				dev_err(&dev->pdev->dev, "I2C payload "
7738c2ecf20Sopenharmony_ci				       "send returned NAK!\n");
7748c2ecf20Sopenharmony_ci				ret = -EIO;
7758c2ecf20Sopenharmony_ci				goto exit_disable;
7768c2ecf20Sopenharmony_ci			}
7778c2ecf20Sopenharmony_ci		}
7788c2ecf20Sopenharmony_ci		if (stop) {
7798c2ecf20Sopenharmony_ci			/* Send stop condition */
7808c2ecf20Sopenharmony_ci			val = I2C_CR_PERIPHERAL_ENABLE;
7818c2ecf20Sopenharmony_ci			val |= I2C_CR_STOP_ENABLE;
7828c2ecf20Sopenharmony_ci			stu300_wr8(val, dev->virtbase + I2C_CR);
7838c2ecf20Sopenharmony_ci		}
7848c2ecf20Sopenharmony_ci	}
7858c2ecf20Sopenharmony_ci
7868c2ecf20Sopenharmony_ci	/* Check that the bus is free, or wait until some timeout occurs */
7878c2ecf20Sopenharmony_ci	ret = stu300_wait_while_busy(dev);
7888c2ecf20Sopenharmony_ci	if (ret != 0) {
7898c2ecf20Sopenharmony_ci		dev_err(&dev->pdev->dev, "timeout waiting for transfer "
7908c2ecf20Sopenharmony_ci		       "to commence.\n");
7918c2ecf20Sopenharmony_ci		goto exit_disable;
7928c2ecf20Sopenharmony_ci	}
7938c2ecf20Sopenharmony_ci
7948c2ecf20Sopenharmony_ci	/* Dummy read status registers */
7958c2ecf20Sopenharmony_ci	val = stu300_r8(dev->virtbase + I2C_SR2);
7968c2ecf20Sopenharmony_ci	val = stu300_r8(dev->virtbase + I2C_SR1);
7978c2ecf20Sopenharmony_ci	ret = 0;
7988c2ecf20Sopenharmony_ci
7998c2ecf20Sopenharmony_ci exit_disable:
8008c2ecf20Sopenharmony_ci	/* Disable controller */
8018c2ecf20Sopenharmony_ci	stu300_wr8(0x00, dev->virtbase + I2C_CR);
8028c2ecf20Sopenharmony_ci	clk_disable(dev->clk);
8038c2ecf20Sopenharmony_ci	return ret;
8048c2ecf20Sopenharmony_ci}
8058c2ecf20Sopenharmony_ci
8068c2ecf20Sopenharmony_cistatic int stu300_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
8078c2ecf20Sopenharmony_ci			 int num)
8088c2ecf20Sopenharmony_ci{
8098c2ecf20Sopenharmony_ci	int ret = -1;
8108c2ecf20Sopenharmony_ci	int i;
8118c2ecf20Sopenharmony_ci
8128c2ecf20Sopenharmony_ci	struct stu300_dev *dev = i2c_get_adapdata(adap);
8138c2ecf20Sopenharmony_ci	dev->msg_len = num;
8148c2ecf20Sopenharmony_ci
8158c2ecf20Sopenharmony_ci	for (i = 0; i < num; i++) {
8168c2ecf20Sopenharmony_ci		/*
8178c2ecf20Sopenharmony_ci		 * Another driver appears to send stop for each message,
8188c2ecf20Sopenharmony_ci		 * here we only do that for the last message. Possibly some
8198c2ecf20Sopenharmony_ci		 * peripherals require this behaviour, then their drivers
8208c2ecf20Sopenharmony_ci		 * have to send single messages in order to get "stop" for
8218c2ecf20Sopenharmony_ci		 * each message.
8228c2ecf20Sopenharmony_ci		 */
8238c2ecf20Sopenharmony_ci		dev->msg_index = i;
8248c2ecf20Sopenharmony_ci
8258c2ecf20Sopenharmony_ci		ret = stu300_xfer_msg(adap, &msgs[i], (i == (num - 1)));
8268c2ecf20Sopenharmony_ci
8278c2ecf20Sopenharmony_ci		if (ret != 0) {
8288c2ecf20Sopenharmony_ci			num = ret;
8298c2ecf20Sopenharmony_ci			break;
8308c2ecf20Sopenharmony_ci		}
8318c2ecf20Sopenharmony_ci	}
8328c2ecf20Sopenharmony_ci
8338c2ecf20Sopenharmony_ci	return num;
8348c2ecf20Sopenharmony_ci}
8358c2ecf20Sopenharmony_ci
8368c2ecf20Sopenharmony_cistatic int stu300_xfer_todo(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
8378c2ecf20Sopenharmony_ci{
8388c2ecf20Sopenharmony_ci	/* TODO: implement polling for this case if need be. */
8398c2ecf20Sopenharmony_ci	WARN(1, "%s: atomic transfers not implemented\n", dev_name(&adap->dev));
8408c2ecf20Sopenharmony_ci	return -EOPNOTSUPP;
8418c2ecf20Sopenharmony_ci}
8428c2ecf20Sopenharmony_ci
8438c2ecf20Sopenharmony_cistatic u32 stu300_func(struct i2c_adapter *adap)
8448c2ecf20Sopenharmony_ci{
8458c2ecf20Sopenharmony_ci	/* This is the simplest thing you can think of... */
8468c2ecf20Sopenharmony_ci	return I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR;
8478c2ecf20Sopenharmony_ci}
8488c2ecf20Sopenharmony_ci
8498c2ecf20Sopenharmony_cistatic const struct i2c_algorithm stu300_algo = {
8508c2ecf20Sopenharmony_ci	.master_xfer = stu300_xfer,
8518c2ecf20Sopenharmony_ci	.master_xfer_atomic = stu300_xfer_todo,
8528c2ecf20Sopenharmony_ci	.functionality = stu300_func,
8538c2ecf20Sopenharmony_ci};
8548c2ecf20Sopenharmony_ci
8558c2ecf20Sopenharmony_cistatic const struct i2c_adapter_quirks stu300_quirks = {
8568c2ecf20Sopenharmony_ci	.flags = I2C_AQ_NO_ZERO_LEN,
8578c2ecf20Sopenharmony_ci};
8588c2ecf20Sopenharmony_ci
8598c2ecf20Sopenharmony_cistatic int stu300_probe(struct platform_device *pdev)
8608c2ecf20Sopenharmony_ci{
8618c2ecf20Sopenharmony_ci	struct stu300_dev *dev;
8628c2ecf20Sopenharmony_ci	struct i2c_adapter *adap;
8638c2ecf20Sopenharmony_ci	int bus_nr;
8648c2ecf20Sopenharmony_ci	int ret = 0;
8658c2ecf20Sopenharmony_ci
8668c2ecf20Sopenharmony_ci	dev = devm_kzalloc(&pdev->dev, sizeof(struct stu300_dev), GFP_KERNEL);
8678c2ecf20Sopenharmony_ci	if (!dev)
8688c2ecf20Sopenharmony_ci		return -ENOMEM;
8698c2ecf20Sopenharmony_ci
8708c2ecf20Sopenharmony_ci	bus_nr = pdev->id;
8718c2ecf20Sopenharmony_ci	dev->clk = devm_clk_get(&pdev->dev, NULL);
8728c2ecf20Sopenharmony_ci	if (IS_ERR(dev->clk)) {
8738c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "could not retrieve i2c bus clock\n");
8748c2ecf20Sopenharmony_ci		return PTR_ERR(dev->clk);
8758c2ecf20Sopenharmony_ci	}
8768c2ecf20Sopenharmony_ci
8778c2ecf20Sopenharmony_ci	dev->pdev = pdev;
8788c2ecf20Sopenharmony_ci	dev->virtbase = devm_platform_ioremap_resource(pdev, 0);
8798c2ecf20Sopenharmony_ci	dev_dbg(&pdev->dev, "initialize bus device I2C%d on virtual "
8808c2ecf20Sopenharmony_ci		"base %p\n", bus_nr, dev->virtbase);
8818c2ecf20Sopenharmony_ci	if (IS_ERR(dev->virtbase))
8828c2ecf20Sopenharmony_ci		return PTR_ERR(dev->virtbase);
8838c2ecf20Sopenharmony_ci
8848c2ecf20Sopenharmony_ci	dev->irq = platform_get_irq(pdev, 0);
8858c2ecf20Sopenharmony_ci	ret = devm_request_irq(&pdev->dev, dev->irq, stu300_irh, 0, NAME, dev);
8868c2ecf20Sopenharmony_ci	if (ret < 0)
8878c2ecf20Sopenharmony_ci		return ret;
8888c2ecf20Sopenharmony_ci
8898c2ecf20Sopenharmony_ci	dev->speed = scl_frequency;
8908c2ecf20Sopenharmony_ci
8918c2ecf20Sopenharmony_ci	clk_prepare_enable(dev->clk);
8928c2ecf20Sopenharmony_ci	ret = stu300_init_hw(dev);
8938c2ecf20Sopenharmony_ci	clk_disable(dev->clk);
8948c2ecf20Sopenharmony_ci	if (ret != 0) {
8958c2ecf20Sopenharmony_ci		dev_err(&dev->pdev->dev, "error initializing hardware.\n");
8968c2ecf20Sopenharmony_ci		return -EIO;
8978c2ecf20Sopenharmony_ci	}
8988c2ecf20Sopenharmony_ci
8998c2ecf20Sopenharmony_ci	/* IRQ event handling initialization */
9008c2ecf20Sopenharmony_ci	spin_lock_init(&dev->cmd_issue_lock);
9018c2ecf20Sopenharmony_ci	dev->cmd_event = STU300_EVENT_NONE;
9028c2ecf20Sopenharmony_ci	dev->cmd_err = STU300_ERROR_NONE;
9038c2ecf20Sopenharmony_ci
9048c2ecf20Sopenharmony_ci	adap = &dev->adapter;
9058c2ecf20Sopenharmony_ci	adap->owner = THIS_MODULE;
9068c2ecf20Sopenharmony_ci	/* DDC class but actually often used for more generic I2C */
9078c2ecf20Sopenharmony_ci	adap->class = I2C_CLASS_DEPRECATED;
9088c2ecf20Sopenharmony_ci	strlcpy(adap->name, "ST Microelectronics DDC I2C adapter",
9098c2ecf20Sopenharmony_ci		sizeof(adap->name));
9108c2ecf20Sopenharmony_ci	adap->nr = bus_nr;
9118c2ecf20Sopenharmony_ci	adap->algo = &stu300_algo;
9128c2ecf20Sopenharmony_ci	adap->dev.parent = &pdev->dev;
9138c2ecf20Sopenharmony_ci	adap->dev.of_node = pdev->dev.of_node;
9148c2ecf20Sopenharmony_ci	adap->quirks = &stu300_quirks;
9158c2ecf20Sopenharmony_ci
9168c2ecf20Sopenharmony_ci	i2c_set_adapdata(adap, dev);
9178c2ecf20Sopenharmony_ci
9188c2ecf20Sopenharmony_ci	/* i2c device drivers may be active on return from add_adapter() */
9198c2ecf20Sopenharmony_ci	ret = i2c_add_numbered_adapter(adap);
9208c2ecf20Sopenharmony_ci	if (ret)
9218c2ecf20Sopenharmony_ci		return ret;
9228c2ecf20Sopenharmony_ci
9238c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, dev);
9248c2ecf20Sopenharmony_ci	dev_info(&pdev->dev, "ST DDC I2C @ %p, irq %d\n",
9258c2ecf20Sopenharmony_ci		 dev->virtbase, dev->irq);
9268c2ecf20Sopenharmony_ci
9278c2ecf20Sopenharmony_ci	return 0;
9288c2ecf20Sopenharmony_ci}
9298c2ecf20Sopenharmony_ci
9308c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP
9318c2ecf20Sopenharmony_cistatic int stu300_suspend(struct device *device)
9328c2ecf20Sopenharmony_ci{
9338c2ecf20Sopenharmony_ci	struct stu300_dev *dev = dev_get_drvdata(device);
9348c2ecf20Sopenharmony_ci
9358c2ecf20Sopenharmony_ci	/* Turn off everything */
9368c2ecf20Sopenharmony_ci	stu300_wr8(0x00, dev->virtbase + I2C_CR);
9378c2ecf20Sopenharmony_ci	return 0;
9388c2ecf20Sopenharmony_ci}
9398c2ecf20Sopenharmony_ci
9408c2ecf20Sopenharmony_cistatic int stu300_resume(struct device *device)
9418c2ecf20Sopenharmony_ci{
9428c2ecf20Sopenharmony_ci	int ret = 0;
9438c2ecf20Sopenharmony_ci	struct stu300_dev *dev = dev_get_drvdata(device);
9448c2ecf20Sopenharmony_ci
9458c2ecf20Sopenharmony_ci	clk_enable(dev->clk);
9468c2ecf20Sopenharmony_ci	ret = stu300_init_hw(dev);
9478c2ecf20Sopenharmony_ci	clk_disable(dev->clk);
9488c2ecf20Sopenharmony_ci
9498c2ecf20Sopenharmony_ci	if (ret != 0)
9508c2ecf20Sopenharmony_ci		dev_err(device, "error re-initializing hardware.\n");
9518c2ecf20Sopenharmony_ci	return ret;
9528c2ecf20Sopenharmony_ci}
9538c2ecf20Sopenharmony_ci
9548c2ecf20Sopenharmony_cistatic SIMPLE_DEV_PM_OPS(stu300_pm, stu300_suspend, stu300_resume);
9558c2ecf20Sopenharmony_ci#define STU300_I2C_PM	(&stu300_pm)
9568c2ecf20Sopenharmony_ci#else
9578c2ecf20Sopenharmony_ci#define STU300_I2C_PM	NULL
9588c2ecf20Sopenharmony_ci#endif
9598c2ecf20Sopenharmony_ci
9608c2ecf20Sopenharmony_cistatic int stu300_remove(struct platform_device *pdev)
9618c2ecf20Sopenharmony_ci{
9628c2ecf20Sopenharmony_ci	struct stu300_dev *dev = platform_get_drvdata(pdev);
9638c2ecf20Sopenharmony_ci
9648c2ecf20Sopenharmony_ci	i2c_del_adapter(&dev->adapter);
9658c2ecf20Sopenharmony_ci	/* Turn off everything */
9668c2ecf20Sopenharmony_ci	stu300_wr8(0x00, dev->virtbase + I2C_CR);
9678c2ecf20Sopenharmony_ci	return 0;
9688c2ecf20Sopenharmony_ci}
9698c2ecf20Sopenharmony_ci
9708c2ecf20Sopenharmony_cistatic const struct of_device_id stu300_dt_match[] = {
9718c2ecf20Sopenharmony_ci	{ .compatible = "st,ddci2c" },
9728c2ecf20Sopenharmony_ci	{},
9738c2ecf20Sopenharmony_ci};
9748c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, stu300_dt_match);
9758c2ecf20Sopenharmony_ci
9768c2ecf20Sopenharmony_cistatic struct platform_driver stu300_i2c_driver = {
9778c2ecf20Sopenharmony_ci	.driver = {
9788c2ecf20Sopenharmony_ci		.name	= NAME,
9798c2ecf20Sopenharmony_ci		.pm	= STU300_I2C_PM,
9808c2ecf20Sopenharmony_ci		.of_match_table = stu300_dt_match,
9818c2ecf20Sopenharmony_ci	},
9828c2ecf20Sopenharmony_ci	.probe = stu300_probe,
9838c2ecf20Sopenharmony_ci	.remove = stu300_remove,
9848c2ecf20Sopenharmony_ci
9858c2ecf20Sopenharmony_ci};
9868c2ecf20Sopenharmony_ci
9878c2ecf20Sopenharmony_cistatic int __init stu300_init(void)
9888c2ecf20Sopenharmony_ci{
9898c2ecf20Sopenharmony_ci	return platform_driver_register(&stu300_i2c_driver);
9908c2ecf20Sopenharmony_ci}
9918c2ecf20Sopenharmony_ci
9928c2ecf20Sopenharmony_cistatic void __exit stu300_exit(void)
9938c2ecf20Sopenharmony_ci{
9948c2ecf20Sopenharmony_ci	platform_driver_unregister(&stu300_i2c_driver);
9958c2ecf20Sopenharmony_ci}
9968c2ecf20Sopenharmony_ci
9978c2ecf20Sopenharmony_ci/*
9988c2ecf20Sopenharmony_ci * The systems using this bus often have very basic devices such
9998c2ecf20Sopenharmony_ci * as regulators on the I2C bus, so this needs to be loaded early.
10008c2ecf20Sopenharmony_ci * Therefore it is registered in the subsys_initcall().
10018c2ecf20Sopenharmony_ci */
10028c2ecf20Sopenharmony_cisubsys_initcall(stu300_init);
10038c2ecf20Sopenharmony_cimodule_exit(stu300_exit);
10048c2ecf20Sopenharmony_ci
10058c2ecf20Sopenharmony_ciMODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
10068c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("ST Micro DDC I2C adapter (" NAME ")");
10078c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
10088c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:" NAME);
1009