tsc2101.c 6.53 KB
Newer Older
Imre Deak's avatar
Imre Deak committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
/*
 * linux/drivers/spi/tsc2101.c
 *
 * TSC2101 codec interface driver for the OMAP platform
 *
 * Copyright (C) 2004 Texas Instruments, Inc.
 *
 * This package is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * History:
 *
 * 2004/11/07   Nishanth Menon - Modified for common hooks for Audio and Touchscreen
 */

#include <linux/kernel.h>
#include <linux/clk.h>
#include <linux/device.h>
#include <linux/spi/spi.h>
#include <linux/spi/tsc2101.h>

struct tsc2101_device {
	struct mutex		mutex;
	int			mclk_enabled;
	struct clock		*mclk_ck;
	struct spi_message	message;
	struct spi_transfer	transfer[2];
	u16			command;
	void			(*enable_mclk)(struct spi_device *spi);
	void			(*disable_mclk)(struct spi_device *spi);
};

int tsc2101_enable_mclk(struct spi_device *spi)
{
	struct tsc2101_device *tsc2101;

	tsc2101 = spi_get_drvdata(spi);

	mutex_lock(&tsc2101->mutex);

	if (spi->dev.power.power_state.event != PM_EVENT_ON) {
		mutex_unlock(&tsc2101->mutex);
		return -ENODEV;
	}

	if (tsc2101->mclk_enabled++ == 0) {
		if (tsc2101->enable_mclk != NULL)
			tsc2101->enable_mclk(spi);
	}

	mutex_unlock(&tsc2101->mutex);
	return 0;
}
EXPORT_SYMBOL(tsc2101_enable_mclk);

void tsc2101_disable_mclk(struct spi_device *spi)
{
	struct tsc2101_device *tsc2101;

	tsc2101 = spi_get_drvdata(spi);

	mutex_lock(&tsc2101->mutex);

	if (--tsc2101->mclk_enabled == 0) {
		if (tsc2101->disable_mclk != NULL &&
		    spi->dev.power.power_state.event == PM_EVENT_ON)
			tsc2101->disable_mclk(spi);
	}

	mutex_lock(&tsc2101->mutex);
}
EXPORT_SYMBOL(tsc2101_disable_mclk);

int tsc2101_write_sync(struct spi_device *spi, int page, u8 address, u16 data)
{
	struct tsc2101_device *tsc2101;
	struct spi_message *m;
	struct spi_transfer *t;
	int ret;

	tsc2101 = spi_get_drvdata(spi);

	mutex_lock(&tsc2101->mutex);
	if (spi->dev.power.power_state.event != PM_EVENT_ON) {
		mutex_unlock(&tsc2101->mutex);
		return -ENODEV;
	}

	m = &tsc2101->message;
	spi_message_init(m);
	t = &tsc2101->transfer[0];
	memset(t, 0, sizeof(tsc2101->transfer));

	/* Address */
	tsc2101->command = (page << 11) | (address << 5);
	t->tx_buf = &tsc2101->command;
	t->len = 2;
	spi_message_add_tail(t, m);

	/* Data */
	t++;
	t->tx_buf = &data;
	t->len = 2;
	spi_message_add_tail(t, m);

	ret = spi_sync(spi, m);
	if (!ret)
		ret = tsc2101->message.status;
	mutex_unlock(&tsc2101->mutex);

	return ret;
}
EXPORT_SYMBOL(tsc2101_write_sync);

int tsc2101_reads_sync(struct spi_device *spi,
		       int page, u8 startaddress, u16 *data, int numregs)
{
	struct tsc2101_device *tsc2101;
	struct spi_message *m;
	struct spi_transfer *t;
	int ret;

	tsc2101 = spi_get_drvdata(spi);

	mutex_lock(&tsc2101->mutex);
	if (spi->dev.power.power_state.event != PM_EVENT_ON) {
		mutex_unlock(&tsc2101->mutex);
		return -ENODEV;
	}

	m = &tsc2101->message;
	spi_message_init(m);
	t = &tsc2101->transfer[0];
	memset(t, 0, sizeof(tsc2101->transfer));

	/* Address */
	tsc2101->command = 0x8000 | (page << 11) | (startaddress << 5);
	t->tx_buf = &tsc2101->command;
	t->len = 2;
	spi_message_add_tail(t, m);

	/* Data */
	t++;
	t->rx_buf = data;
	t->len = numregs << 1;
	spi_message_add_tail(t, m);

	ret = spi_sync(spi, m);
	if (!ret)
		ret = tsc2101->message.status;

	mutex_unlock(&tsc2101->mutex);

	return ret;
}
EXPORT_SYMBOL(tsc2101_reads_sync);

int tsc2101_read_sync(struct spi_device *spi, int page, u8 address)
{
	int err;
	u16 val;

	err = tsc2101_reads_sync(spi, page, address, &val, 1);
	if (err)
		return err;
	return val;
}
EXPORT_SYMBOL(tsc2101_read_sync);

static int tsc2101_suspend(struct spi_device *spi, pm_message_t state)
{
	struct tsc2101_device *tsc2101;

	tsc2101 = spi_get_drvdata(spi);

	if (tsc2101 == NULL)
		return 0;

	mutex_lock(&tsc2101->mutex);

	spi->dev.power.power_state = state;
	if (tsc2101->mclk_enabled && tsc2101->disable_mclk != NULL)
		tsc2101->disable_mclk(spi);

	mutex_unlock(&tsc2101->mutex);

	return 0;
}

static int tsc2101_resume(struct spi_device *spi)
{
	struct tsc2101_device *tsc2101;

	tsc2101 = spi_get_drvdata(spi);

	if (tsc2101 == NULL)
		return 0;

	mutex_lock(&tsc2101->mutex);

	spi->dev.power.power_state = PMSG_ON;
	if (tsc2101->mclk_enabled && tsc2101->enable_mclk != NULL)
		tsc2101->enable_mclk(spi);

	mutex_unlock(&tsc2101->mutex);

	return 0;
}

static int tsc2101_probe(struct spi_device *spi)
{
	struct tsc2101_platform_data *pdata;
	struct tsc2101_device *tsc2101;
	u16 w;
	int r;

	pdata = spi->dev.platform_data;
	if (pdata == NULL) {
		dev_err(&spi->dev, "no platform data\n");
		return -ENODEV;
	}

	tsc2101 = kzalloc(sizeof(*tsc2101), GFP_KERNEL);
	if (tsc2101 == NULL) {
		dev_err(&spi->dev, "out of mem\n");
		return -ENOMEM;
	}

	spi_set_drvdata(spi, tsc2101);
	tsc2101->enable_mclk = pdata->enable_mclk;
	tsc2101->disable_mclk = pdata->disable_mclk;

	mutex_init(&tsc2101->mutex);

	spi->mode = SPI_MODE_0;
	spi->bits_per_word = 16;
	if ((r = spi_setup(spi)) < 0) {
		dev_err(&spi->dev, "SPI setup failed\n");
		goto err;
	}

	w = tsc2101_read_sync(spi, 1, 0);
	if (!(w & (1 << 14))) {
		dev_err(&spi->dev, "invalid ADC register value %04x\n", w);
		goto err;
	}

	if (pdata->init != NULL) {
		if ((r = pdata->init(spi)) < 0) {
			dev_err(&spi->dev, "platform init failed\n");
			goto err;
		}
	}

	dev_info(&spi->dev, "initialized\n");

	return 0;
err:
	kfree(tsc2101);
	return r;
}

static int tsc2101_remove(struct spi_device *spi)
{
	struct tsc2101_platform_data *pdata;
	struct tsc2101_device *tsc2101;

	pdata = spi->dev.platform_data;
	tsc2101 = spi_get_drvdata(spi);

	/* We assume that this can't race with the rest of the driver. */
	if (tsc2101->mclk_enabled && tsc2101->disable_mclk != NULL)
		tsc2101->disable_mclk(spi);

	if (pdata->cleanup != NULL)
		pdata->cleanup(spi);

	spi_set_drvdata(spi, NULL);
	kfree(tsc2101);

	return 0;
}

static struct spi_driver tsc2101_driver = {
	.probe		= tsc2101_probe,
	.remove		= tsc2101_remove,
	.suspend	= tsc2101_suspend,
	.resume		= tsc2101_resume,
	.driver		= {
		.name	= "tsc2101",
		.owner	= THIS_MODULE,
	},
};

static int tsc2101_init(void)
{
	return spi_register_driver(&tsc2101_driver);
}

static void tsc2101_exit(void)
{
	spi_unregister_driver(&tsc2101_driver);
}

module_init(tsc2101_init);
module_exit(tsc2101_exit);

MODULE_AUTHOR("Texas Instruments");
MODULE_DESCRIPTION
    ("Glue audio driver for the TI OMAP1610/OMAP1710 TSC2101 codec.");
MODULE_LICENSE("GPL");