blob: b7d38cd7f83f749a52f6273e6088468f48c22a39 [file] [log] [blame]
Stephen Boydaf0be7e2012-04-25 11:52:18 -07001/* Copyright (c) 2009-2012, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13/*
14 * Qualcomm Marimba Core Driver
15 */
16
17#include <linux/kernel.h>
18#include <linux/init.h>
19#include <linux/mutex.h>
20#include <linux/platform_device.h>
21#include <linux/err.h>
22
23#include <linux/i2c.h>
24#include <linux/mfd/marimba.h>
Vijayakumar Muthuvel Manickam9a9e76e2011-06-30 11:22:25 -070025#include <linux/slab.h>
26#include <linux/debugfs.h>
Steve Mucklef132c6c2012-06-06 18:30:57 -070027#include <linux/module.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070028
29#define MARIMBA_MODE 0x00
30
31#define ADIE_ARRY_SIZE (CHIP_ID_MAX * MARIMBA_NUM_CHILD)
32
33static int marimba_shadow[ADIE_ARRY_SIZE][0xff];
Rahul Kashyapba345d52011-07-14 17:31:53 +053034static int mutex_initialized;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070035struct marimba marimba_modules[ADIE_ARRY_SIZE];
36
37#define MARIMBA_VERSION_REG 0x11
38#define MARIMBA_MODE_REG 0x00
39
40struct marimba_platform_data *marimba_pdata;
41
42static uint32_t marimba_gpio_count;
43static bool fm_status;
44static bool bt_status;
45
46#ifdef CONFIG_I2C_SSBI
47#define NUM_ADD MARIMBA_NUM_CHILD
48#else
49#define NUM_ADD (MARIMBA_NUM_CHILD - 1)
50#endif
51
Vijayakumar Muthuvel Manickam9a9e76e2011-06-30 11:22:25 -070052#if defined(CONFIG_DEBUG_FS)
53struct adie_dbg_device {
54 struct mutex dbg_mutex;
55 struct dentry *dent;
56 int addr;
57 int mod_id;
58};
59
60static struct adie_dbg_device *marimba_dbg_device;
61static struct adie_dbg_device *timpani_dbg_device;
62static struct adie_dbg_device *bahama_dbg_device;
63#endif
64
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070065
66/**
67 * marimba_read_bahama_ver - Reads Bahama version.
68 * @param marimba: marimba structure pointer passed by client
69 * @returns result of the operation.
70 */
71int marimba_read_bahama_ver(struct marimba *marimba)
72{
73 int rc;
74 u8 bahama_version;
75
76 rc = marimba_read_bit_mask(marimba, 0x00, &bahama_version, 1, 0x1F);
77 if (rc < 0)
78 return rc;
Venkateshwarlu Domakonda4fba7862012-10-01 17:54:23 +053079 pr_debug("%s: Bahama version: 0x%x\n", __func__, bahama_version);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070080 switch (bahama_version) {
81 case 0x08: /* varient of bahama v1 */
82 case 0x10:
83 case 0x00:
84 return BAHAMA_VER_1_0;
85 case 0x09: /* variant of bahama v2 */
Venkateshwarlu Domakonda4fba7862012-10-01 17:54:23 +053086 case 0x0a: /* variant of bahama v2.1 */
87 /* Falling through because initialization */
88 /* and configuration for 2.0 and 2.1 are same */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070089 return BAHAMA_VER_2_0;
90 default:
91 return BAHAMA_VER_UNSUPPORTED;
92 }
93}
94EXPORT_SYMBOL(marimba_read_bahama_ver);
95/**
96 * marimba_ssbi_write - Writes a n bit TSADC register in Marimba
97 * @param marimba: marimba structure pointer passed by client
98 * @param reg: register address
99 * @param value: buffer to be written
100 * @param len: num of bytes
101 * @returns result of the operation.
102 */
103int marimba_ssbi_write(struct marimba *marimba, u16 reg , u8 *value, int len)
104{
105 struct i2c_msg *msg;
106 int ret;
107
108 marimba = &marimba_modules[marimba->mod_id];
109
110 mutex_lock(&marimba->xfer_lock);
111
112 msg = &marimba->xfer_msg[0];
113 msg->addr = reg;
114 msg->flags = 0x0;
115 msg->buf = value;
116 msg->len = len;
117
118 ret = i2c_transfer(marimba->client->adapter, marimba->xfer_msg, 1);
119
120 mutex_unlock(&marimba->xfer_lock);
121
122 return ret;
123}
124EXPORT_SYMBOL(marimba_ssbi_write);
125
126/**
127 * marimba_ssbi_read - Reads a n bit TSADC register in Marimba
128 * @param marimba: marimba structure pointer passed by client
129 * @param reg: register address
130 * @param value: ssbi read of the register to be stored
131 * @param len: num of bytes
132 *
133 * @returns result of the operation.
134*/
135int marimba_ssbi_read(struct marimba *marimba, u16 reg, u8 *value, int len)
136{
137 struct i2c_msg *msg;
138 int ret;
139
140 marimba = &marimba_modules[marimba->mod_id];
141
142 mutex_lock(&marimba->xfer_lock);
143
144 msg = &marimba->xfer_msg[0];
145 msg->addr = reg;
146 msg->flags = I2C_M_RD;
147 msg->buf = value;
148 msg->len = len;
149
150 ret = i2c_transfer(marimba->client->adapter, marimba->xfer_msg, 1);
151
152 mutex_unlock(&marimba->xfer_lock);
153
154 return ret;
155}
156EXPORT_SYMBOL(marimba_ssbi_read);
157
158/**
159 * marimba_write_bit_mask - Sets n bit register using bit mask
160 * @param marimba: marimba structure pointer passed by client
161 * @param reg: register address
162 * @param value: buffer to be written to the registers
163 * @param num_bytes: n bytes to write
164 * @param mask: bit mask corresponding to the registers
165 *
166 * @returns result of the operation.
167 */
168int marimba_write_bit_mask(struct marimba *marimba, u8 reg, u8 *value,
169 unsigned num_bytes, u8 mask)
170{
171 int ret, i;
172 struct i2c_msg *msg;
173 u8 data[num_bytes + 1];
174 u8 mask_value[num_bytes];
175
176 marimba = &marimba_modules[marimba->mod_id];
Venkateshwarlu Domakonda00c882d2012-04-12 10:50:53 +0530177 if (marimba == NULL) {
178 pr_err("%s: Unable to access Marimba core\n", __func__);
179 return -ENODEV;
180 }
181
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700182
183 mutex_lock(&marimba->xfer_lock);
184
185 for (i = 0; i < num_bytes; i++)
186 mask_value[i] = (marimba_shadow[marimba->mod_id][reg + i]
187 & ~mask) | (value[i] & mask);
188
189 msg = &marimba->xfer_msg[0];
Venkateshwarlu Domakonda00c882d2012-04-12 10:50:53 +0530190 if (marimba->client == NULL) {
191 pr_err("%s: Unable to access the Marimba slave device.\n",
192 __func__);
193 return -ENODEV;
194 }
195
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700196 msg->addr = marimba->client->addr;
197 msg->flags = 0;
198 msg->len = num_bytes + 1;
199 msg->buf = data;
200 data[0] = reg;
201 memcpy(data+1, mask_value, num_bytes);
202
203 ret = i2c_transfer(marimba->client->adapter, marimba->xfer_msg, 1);
204
205 /* Try again if the write fails */
206 if (ret != 1)
207 ret = i2c_transfer(marimba->client->adapter,
208 marimba->xfer_msg, 1);
209
210 if (ret == 1) {
211 for (i = 0; i < num_bytes; i++)
212 marimba_shadow[marimba->mod_id][reg + i]
213 = mask_value[i];
Ankit Vermad7834992011-08-22 12:29:33 +0530214 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700215 dev_err(&marimba->client->dev, "i2c write failed\n");
Ankit Vermad7834992011-08-22 12:29:33 +0530216 ret = -ENODEV;
217 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700218
219 mutex_unlock(&marimba->xfer_lock);
220
221 return ret;
222}
223EXPORT_SYMBOL(marimba_write_bit_mask);
224
225/**
226 * marimba_write - Sets n bit register in Marimba
227 * @param marimba: marimba structure pointer passed by client
228 * @param reg: register address
229 * @param value: buffer values to be written
230 * @param num_bytes: n bytes to write
231 *
232 * @returns result of the operation.
233 */
234int marimba_write(struct marimba *marimba, u8 reg, u8 *value,
235 unsigned num_bytes)
236{
237 return marimba_write_bit_mask(marimba, reg, value, num_bytes, 0xff);
238}
239EXPORT_SYMBOL(marimba_write);
240
241/**
242 * marimba_read_bit_mask - Reads a n bit register based on bit mask
243 * @param marimba: marimba structure pointer passed by client
244 * @param reg: register address
245 * @param value: i2c read of the register to be stored
246 * @param num_bytes: n bytes to be read.
247 * @param mask: bit mask concerning its register
248 *
249 * @returns result of the operation.
250*/
251int marimba_read_bit_mask(struct marimba *marimba, u8 reg, u8 *value,
252 unsigned num_bytes, u8 mask)
253{
254 int ret, i;
255
256 struct i2c_msg *msg;
257
258 marimba = &marimba_modules[marimba->mod_id];
259
260 mutex_lock(&marimba->xfer_lock);
261
262 msg = &marimba->xfer_msg[0];
263 msg->addr = marimba->client->addr;
264 msg->len = 1;
265 msg->flags = 0;
266 msg->buf = &reg;
267
268 msg = &marimba->xfer_msg[1];
269 msg->addr = marimba->client->addr;
270 msg->len = num_bytes;
271 msg->flags = I2C_M_RD;
272 msg->buf = value;
273
274 ret = i2c_transfer(marimba->client->adapter, marimba->xfer_msg, 2);
275
276 /* Try again if read fails first time */
277 if (ret != 2)
278 ret = i2c_transfer(marimba->client->adapter,
279 marimba->xfer_msg, 2);
280
281 if (ret == 2) {
282 for (i = 0; i < num_bytes; i++) {
283 marimba_shadow[marimba->mod_id][reg + i] = value[i];
284 value[i] &= mask;
285 }
Ankit Vermad7834992011-08-22 12:29:33 +0530286 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700287 dev_err(&marimba->client->dev, "i2c read failed\n");
Ankit Vermad7834992011-08-22 12:29:33 +0530288 ret = -ENODEV;
289 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700290
291 mutex_unlock(&marimba->xfer_lock);
292
293 return ret;
294}
295EXPORT_SYMBOL(marimba_read_bit_mask);
296
297/**
298 * marimba_read - Reads n bit registers in Marimba
299 * @param marimba: marimba structure pointer passed by client
300 * @param reg: register address
301 * @param value: i2c read of the register to be stored
302 * @param num_bytes: n bytes to read.
303 * @param mask: bit mask concerning its register
304 *
305 * @returns result of the operation.
306*/
307int marimba_read(struct marimba *marimba, u8 reg, u8 *value, unsigned num_bytes)
308{
309 return marimba_read_bit_mask(marimba, reg, value, num_bytes, 0xff);
310}
311EXPORT_SYMBOL(marimba_read);
312
313int timpani_read(struct marimba *marimba, u8 reg, u8 *value, unsigned num_bytes)
314{
315 return marimba_read_bit_mask(marimba, reg, value, num_bytes, 0xff);
316}
317EXPORT_SYMBOL(timpani_read);
318
319int timpani_write(struct marimba *marimba, u8 reg,
320 u8 *value, unsigned num_bytes)
321{
322 return marimba_write_bit_mask(marimba, reg, value, num_bytes, 0xff);
323}
324EXPORT_SYMBOL(timpani_write);
325
326static int cur_codec_type = -1, cur_adie_type = -1, cur_connv_type = -1;
327static int adie_arry_idx;
328
329int adie_get_detected_codec_type(void)
330{
331 return cur_codec_type;
332}
333EXPORT_SYMBOL(adie_get_detected_codec_type);
334
335int adie_get_detected_connectivity_type(void)
336{
337 return cur_connv_type;
338}
339EXPORT_SYMBOL(adie_get_detected_connectivity_type);
340
341static struct device *
342add_numbered_child(unsigned chip, const char *name, int num, u8 driver_data,
343 void *pdata, unsigned pdata_len)
344{
345 struct platform_device *pdev;
346 struct marimba *marimba = &marimba_modules[chip + adie_arry_idx];
347 int status = 0;
348
349 pdev = platform_device_alloc(name, num);
350 if (!pdev) {
351 status = -ENOMEM;
352 return ERR_PTR(status);
353 }
354
355 pdev->dev.parent = &marimba->client->dev;
356
357 marimba->mod_id = chip + adie_arry_idx;
358
359 platform_set_drvdata(pdev, marimba);
360
361 if (pdata) {
362 status = platform_device_add_data(pdev, pdata, pdata_len);
363 if (status < 0)
364 goto err;
365 }
366
367 status = platform_device_add(pdev);
368 if (status < 0)
369 goto err;
370
371err:
372 if (status < 0) {
373 platform_set_drvdata(pdev, NULL);
374 platform_device_put(pdev);
375 dev_err(&marimba->client->dev, "can't add %s dev\n", name);
376 return ERR_PTR(status);
377 }
378 return &pdev->dev;
379}
380
381static inline struct device *add_child(unsigned chip, const char *name,
382 u8 driver_data, void *pdata, unsigned pdata_len)
383{
384 return add_numbered_child(chip, name, -1, driver_data, pdata,
385 pdata_len);
386}
387
388static int marimba_add_child(struct marimba_platform_data *pdata,
389 u8 driver_data)
390{
391 struct device *child;
392
393 if (cur_adie_type == MARIMBA_ID) {
394 child = add_child(MARIMBA_SLAVE_ID_FM, "marimba_fm",
395 driver_data, pdata->fm, sizeof(*pdata->fm));
396 if (IS_ERR(child))
397 return PTR_ERR(child);
398 } else if ((cur_adie_type == BAHAMA_ID) &&
399 (cur_connv_type == BAHAMA_ID)) {
400 child = add_child(BAHAMA_SLAVE_ID_FM_ID, "marimba_fm",
401 driver_data, pdata->fm, sizeof(*pdata->fm));
402 if (IS_ERR(child))
403 return PTR_ERR(child);
404 }
405
406 /* Add Codec for Marimba and Timpani */
407 if (cur_adie_type == MARIMBA_ID) {
408 child = add_child(MARIMBA_SLAVE_ID_CDC, "marimba_codec",
409 driver_data, pdata->codec, sizeof(*pdata->codec));
410 if (IS_ERR(child))
411 return PTR_ERR(child);
412 } else if (cur_adie_type == TIMPANI_ID) {
413 child = add_child(MARIMBA_SLAVE_ID_CDC, "timpani_codec",
414 driver_data, pdata->codec, sizeof(*pdata->codec));
415 if (IS_ERR(child))
416 return PTR_ERR(child);
417 }
418
419#if defined(CONFIG_I2C_SSBI)
420 if ((pdata->tsadc != NULL) && (cur_adie_type != BAHAMA_ID)) {
421 child = add_child(MARIMBA_ID_TSADC, "marimba_tsadc",
422 driver_data, pdata->tsadc, sizeof(*pdata->tsadc));
423 if (IS_ERR(child))
424 return PTR_ERR(child);
425 }
426#endif
427 return 0;
428}
429
430int marimba_gpio_config(int gpio_value)
431{
432 struct marimba *marimba = &marimba_modules[MARIMBA_SLAVE_ID_MARIMBA];
433 struct marimba_platform_data *pdata = marimba_pdata;
434 int rc = 0;
435
436 /* Clients BT/FM need to manage GPIO 34 on Fusion for its clocks */
437
438 mutex_lock(&marimba->xfer_lock);
439
440 if (gpio_value) {
441 marimba_gpio_count++;
442 if (marimba_gpio_count == 1)
443 rc = pdata->marimba_gpio_config(1);
444 } else {
445 marimba_gpio_count--;
446 if (marimba_gpio_count == 0)
447 rc = pdata->marimba_gpio_config(0);
448 }
449
450 mutex_unlock(&marimba->xfer_lock);
451
452 return rc;
453
454}
455EXPORT_SYMBOL(marimba_gpio_config);
456
457bool marimba_get_fm_status(struct marimba *marimba)
458{
459 bool ret;
460
461 marimba = &marimba_modules[marimba->mod_id];
462
463 mutex_lock(&marimba->xfer_lock);
464
465 ret = fm_status;
466
467 mutex_unlock(&marimba->xfer_lock);
468
469 return ret;
470}
471EXPORT_SYMBOL(marimba_get_fm_status);
472
473void marimba_set_fm_status(struct marimba *marimba, bool value)
474{
475 marimba = &marimba_modules[marimba->mod_id];
476
477 mutex_lock(&marimba->xfer_lock);
478
479 fm_status = value;
480
481 mutex_unlock(&marimba->xfer_lock);
482}
483EXPORT_SYMBOL(marimba_set_fm_status);
484
485bool marimba_get_bt_status(struct marimba *marimba)
486{
487 bool ret;
488
489 marimba = &marimba_modules[marimba->mod_id];
490
491 mutex_lock(&marimba->xfer_lock);
492
493 ret = bt_status;
494
495 mutex_unlock(&marimba->xfer_lock);
496
497 return ret;
498}
499EXPORT_SYMBOL(marimba_get_bt_status);
500
501void marimba_set_bt_status(struct marimba *marimba, bool value)
502{
503 marimba = &marimba_modules[marimba->mod_id];
504
505 mutex_lock(&marimba->xfer_lock);
506
507 bt_status = value;
508
509 mutex_unlock(&marimba->xfer_lock);
510}
511EXPORT_SYMBOL(marimba_set_bt_status);
512
Vijayakumar Muthuvel Manickam9a9e76e2011-06-30 11:22:25 -0700513#if defined(CONFIG_DEBUG_FS)
514
515static int check_addr(int addr, const char *func_name)
516{
517 if (addr < 0 || addr > 0xFF) {
518 pr_err("%s: Marimba register address is invalid: %d\n",
519 func_name, addr);
520 return -EINVAL;
521 }
522 return 0;
523}
524
525static int marimba_debugfs_set(void *data, u64 val)
526{
527 struct adie_dbg_device *dbgdev = data;
528 u8 reg = val;
529 int rc;
530 struct marimba marimba_id;
531
532 mutex_lock(&dbgdev->dbg_mutex);
533
534 rc = check_addr(dbgdev->addr, __func__);
535 if (rc)
536 goto done;
537
538 marimba_id.mod_id = dbgdev->mod_id;
539 rc = marimba_write(&marimba_id, dbgdev->addr, &reg, 1);
540 rc = (rc == 1) ? 0 : rc;
541
542 if (rc)
543 pr_err("%s: FAIL marimba_write(0x%03X)=0x%02X: rc=%d\n",
544 __func__, dbgdev->addr, reg, rc);
545done:
546 mutex_unlock(&dbgdev->dbg_mutex);
547 return rc;
548}
549
550static int marimba_debugfs_get(void *data, u64 *val)
551{
552 struct adie_dbg_device *dbgdev = data;
553 int rc;
554 u8 reg;
555 struct marimba marimba_id;
556
557 mutex_lock(&dbgdev->dbg_mutex);
558
559 rc = check_addr(dbgdev->addr, __func__);
560 if (rc)
561 goto done;
562
563 marimba_id.mod_id = dbgdev->mod_id;
564 rc = marimba_read(&marimba_id, dbgdev->addr, &reg, 1);
565 rc = (rc == 2) ? 0 : rc;
566
567 if (rc) {
568 pr_err("%s: FAIL marimba_read(0x%03X)=0x%02X: rc=%d\n",
569 __func__, dbgdev->addr, reg, rc);
570 goto done;
571 }
572
573 *val = reg;
574done:
575 mutex_unlock(&dbgdev->dbg_mutex);
576 return rc;
577}
578
579DEFINE_SIMPLE_ATTRIBUTE(dbg_marimba_fops, marimba_debugfs_get,
580 marimba_debugfs_set, "0x%02llX\n");
581
582static int addr_set(void *data, u64 val)
583{
584 struct adie_dbg_device *dbgdev = data;
585 int rc;
586
587 rc = check_addr(val, __func__);
588 if (rc)
589 return rc;
590
591 mutex_lock(&dbgdev->dbg_mutex);
592 dbgdev->addr = val;
593 mutex_unlock(&dbgdev->dbg_mutex);
594
595 return 0;
596}
597
598static int addr_get(void *data, u64 *val)
599{
600 struct adie_dbg_device *dbgdev = data;
601 int rc;
602
603 mutex_lock(&dbgdev->dbg_mutex);
604
605 rc = check_addr(dbgdev->addr, __func__);
606 if (rc) {
607 mutex_unlock(&dbgdev->dbg_mutex);
608 return rc;
609 }
610 *val = dbgdev->addr;
611
612 mutex_unlock(&dbgdev->dbg_mutex);
613
614 return 0;
615}
616
617DEFINE_SIMPLE_ATTRIBUTE(dbg_addr_fops, addr_get, addr_set, "0x%03llX\n");
618
619static int __devinit marimba_dbg_init(int adie_type)
620{
621 struct adie_dbg_device *dbgdev;
622 struct dentry *dent;
623 struct dentry *temp;
624
625 dbgdev = kzalloc(sizeof *dbgdev, GFP_KERNEL);
626 if (dbgdev == NULL) {
627 pr_err("%s: kzalloc() failed.\n", __func__);
628 return -ENOMEM;
629 }
630
631 mutex_init(&dbgdev->dbg_mutex);
632 dbgdev->addr = -1;
633
634 if (adie_type == MARIMBA_ID) {
635 marimba_dbg_device = dbgdev;
636 marimba_dbg_device->mod_id = MARIMBA_SLAVE_ID_MARIMBA;
637 dent = debugfs_create_dir("marimba-dbg", NULL);
638 } else if (adie_type == TIMPANI_ID) {
639 timpani_dbg_device = dbgdev;
640 timpani_dbg_device->mod_id = MARIMBA_SLAVE_ID_MARIMBA;
641 dent = debugfs_create_dir("timpani-dbg", NULL);
642 } else if (adie_type == BAHAMA_ID) {
643 bahama_dbg_device = dbgdev;
644 bahama_dbg_device->mod_id = SLAVE_ID_BAHAMA;
645 dent = debugfs_create_dir("bahama-dbg", NULL);
646 }
647 if (dent == NULL || IS_ERR(dent)) {
648 pr_err("%s: ERR debugfs_create_dir: dent=0x%X\n",
649 __func__, (unsigned)dent);
650 kfree(dbgdev);
651 return -ENOMEM;
652 }
653
654 temp = debugfs_create_file("addr", S_IRUSR | S_IWUSR, dent,
655 dbgdev, &dbg_addr_fops);
656 if (temp == NULL || IS_ERR(temp)) {
657 pr_err("%s: ERR debugfs_create_file: dent=0x%X\n",
658 __func__, (unsigned)temp);
659 goto debug_error;
660 }
661
662 temp = debugfs_create_file("data", S_IRUSR | S_IWUSR, dent,
663 dbgdev, &dbg_marimba_fops);
664 if (temp == NULL || IS_ERR(temp)) {
665 pr_err("%s: ERR debugfs_create_file: dent=0x%X\n",
666 __func__, (unsigned)temp);
667 goto debug_error;
668 }
669 dbgdev->dent = dent;
670
671 return 0;
672
673debug_error:
674 kfree(dbgdev);
675 debugfs_remove_recursive(dent);
676 return -ENOMEM;
677}
678
679static int __devexit marimba_dbg_remove(void)
680{
681 if (marimba_dbg_device) {
682 debugfs_remove_recursive(marimba_dbg_device->dent);
683 kfree(marimba_dbg_device);
684 }
685 if (timpani_dbg_device) {
686 debugfs_remove_recursive(timpani_dbg_device->dent);
687 kfree(timpani_dbg_device);
688 }
689 if (bahama_dbg_device) {
690 debugfs_remove_recursive(bahama_dbg_device->dent);
691 kfree(bahama_dbg_device);
692 }
693 return 0;
694}
695
696#else
697
698static int __devinit marimba_dbg_init(int adie_type)
699{
700 return 0;
701}
702
703static int __devexit marimba_dbg_remove(void)
704{
705 return 0;
706}
707
708#endif
709
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700710static int get_adie_type(void)
711{
712 u8 rd_val;
713 int ret;
714
715 struct marimba *marimba = &marimba_modules[ADIE_ARRY_SIZE - 1];
716
717 marimba->mod_id = ADIE_ARRY_SIZE - 1;
718 /* Enable the Mode for Marimba/Timpani */
719 ret = marimba_read(marimba, MARIMBA_MODE_REG, &rd_val, 1);
720
721 if (ret >= 0) {
722 if (rd_val & 0x80) {
723 cur_adie_type = BAHAMA_ID;
724 return cur_adie_type;
725 } else {
726 ret = marimba_read(marimba,
727 MARIMBA_VERSION_REG, &rd_val, 1);
728 if ((ret >= 0) && (rd_val & 0x20)) {
729 cur_adie_type = TIMPANI_ID;
730 return cur_adie_type;
731 } else if (ret >= 0) {
732 cur_adie_type = MARIMBA_ID;
733 return cur_adie_type;
734 }
735 }
736 }
737
738 return ret;
739}
740
741static void marimba_init_reg(struct i2c_client *client, u8 driver_data)
742{
743 struct marimba_platform_data *pdata = client->dev.platform_data;
744 struct marimba *marimba =
745 &marimba_modules[MARIMBA_SLAVE_ID_MARIMBA + adie_arry_idx];
746
747 u8 buf[1];
748
749 buf[0] = 0x10;
750
751 if (cur_adie_type != BAHAMA_ID) {
752 marimba->mod_id = MARIMBA_SLAVE_ID_MARIMBA + adie_arry_idx;
753 /* Enable the Mode for Marimba/Timpani */
754 marimba_write(marimba, MARIMBA_MODE, buf, 1);
755 } else if ((cur_adie_type == BAHAMA_ID) &&
756 (cur_connv_type == BAHAMA_ID)) {
757 marimba->mod_id = MARIMBA_SLAVE_ID_MARIMBA + adie_arry_idx;
758 marimba_write(marimba, BAHAMA_SLAVE_ID_FM_ID,
759 &pdata->slave_id[SLAVE_ID_BAHAMA_FM], 1);
760 /* Configure Bahama core registers (AREG & DREG) */
761 /* with optimal values to eliminate power leakage */
762 if (pdata->bahama_core_config != NULL)
763 pdata->bahama_core_config(cur_adie_type);
764 }
765}
766
Stephen Boydaf0be7e2012-04-25 11:52:18 -0700767static int __devinit marimba_probe(struct i2c_client *client,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700768 const struct i2c_device_id *id)
769{
770 struct marimba_platform_data *pdata = client->dev.platform_data;
771 struct i2c_adapter *ssbi_adap;
772 struct marimba *marimba;
773 int i, status, rc, client_loop, adie_slave_idx_offset;
774 int rc_bahama = 0, rc_marimba = 0;
775
776 if (!pdata) {
777 dev_dbg(&client->dev, "no platform data?\n");
Rahul Kashyapba345d52011-07-14 17:31:53 +0530778 status = -EINVAL;
779 goto fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700780 }
781
782 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) == 0) {
783 dev_dbg(&client->dev, "can't talk I2C?\n");
Rahul Kashyapba345d52011-07-14 17:31:53 +0530784 status = -EIO;
785 goto fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700786 }
Rahul Kashyapba345d52011-07-14 17:31:53 +0530787 if (!mutex_initialized) {
788 for (i = 0; i < ADIE_ARRY_SIZE; ++i) {
789 marimba = &marimba_modules[i];
790 mutex_init(&marimba->xfer_lock);
791 }
792 mutex_initialized = 1;
793 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700794 /* First, identify the codec type */
795 if (pdata->marimba_setup != NULL) {
796 rc_marimba = pdata->marimba_setup();
797 if (rc_marimba)
798 pdata->marimba_shutdown();
799 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700800 if (pdata->bahama_setup != NULL &&
801 cur_connv_type != BAHAMA_ID) {
802 rc_bahama = pdata->bahama_setup();
803 if (rc_bahama)
804 pdata->bahama_shutdown(cur_connv_type);
805 }
Rahul Kashyapba345d52011-07-14 17:31:53 +0530806 if (rc_marimba & rc_bahama) {
807 status = -EAGAIN;
808 goto fail;
809 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700810 marimba = &marimba_modules[ADIE_ARRY_SIZE - 1];
811 marimba->client = client;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700812
813 rc = get_adie_type();
814
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700815 if (rc < 0) {
816 if (pdata->bahama_setup != NULL)
817 pdata->bahama_shutdown(cur_adie_type);
818 if (pdata->marimba_shutdown != NULL)
819 pdata->marimba_shutdown();
Rahul Kashyapba345d52011-07-14 17:31:53 +0530820 status = -ENODEV;
821 goto fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700822 }
823
824 if (rc < 2) {
825 adie_arry_idx = 0;
826 adie_slave_idx_offset = 0;
827 client_loop = 0;
828 cur_codec_type = rc;
829 if (cur_connv_type < 0)
830 cur_connv_type = rc;
831 if (pdata->bahama_shutdown != NULL)
832 pdata->bahama_shutdown(cur_connv_type);
833 } else {
834 adie_arry_idx = 5;
835 adie_slave_idx_offset = 5;
836 client_loop = 1;
837 cur_connv_type = rc;
838 }
839
840 marimba = &marimba_modules[adie_arry_idx];
841 marimba->client = client;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700842
843 for (i = 1; i <= (NUM_ADD - client_loop); i++) {
844 /* Skip adding BT/FM for Timpani */
845 if (i == 1 && rc >= 1)
846 i++;
847 marimba = &marimba_modules[i + adie_arry_idx];
848 if (i != MARIMBA_ID_TSADC)
849 marimba->client = i2c_new_dummy(client->adapter,
850 pdata->slave_id[i + adie_slave_idx_offset]);
851 else if (pdata->tsadc_ssbi_adap) {
852 ssbi_adap = i2c_get_adapter(pdata->tsadc_ssbi_adap);
853 marimba->client = i2c_new_dummy(ssbi_adap,
854 0x55);
855 } else
856 ssbi_adap = NULL;
857
858 if (!marimba->client) {
Juffin Alex Varghese1949cbd2012-10-29 18:48:40 +0530859 pr_err("can't attach client %d\n", i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700860 status = -ENOMEM;
861 goto fail;
862 }
863 strlcpy(marimba->client->name, id->name,
864 sizeof(marimba->client->name));
865
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700866 }
867
Vijayakumar Muthuvel Manickam9a9e76e2011-06-30 11:22:25 -0700868 if (marimba_dbg_init(rc) != 0)
869 pr_debug("%s: marimba debugfs init failed\n", __func__);
870
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700871 marimba_init_reg(client, id->driver_data);
872
873 status = marimba_add_child(pdata, id->driver_data);
874
875 marimba_pdata = pdata;
876
877 return 0;
878
879fail:
880 return status;
881}
882
883static int __devexit marimba_remove(struct i2c_client *client)
884{
885 int i;
886 struct marimba_platform_data *pdata;
887
888 pdata = client->dev.platform_data;
Rahul Kashyapba345d52011-07-14 17:31:53 +0530889 for (i = 0; i < ADIE_ARRY_SIZE; i++) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700890 struct marimba *marimba = &marimba_modules[i];
891
892 if (marimba->client && marimba->client != client)
893 i2c_unregister_device(marimba->client);
894
895 marimba_modules[i].client = NULL;
Rahul Kashyapba345d52011-07-14 17:31:53 +0530896 if (mutex_initialized)
897 mutex_destroy(&marimba->xfer_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700898
Rahul Kashyapba345d52011-07-14 17:31:53 +0530899 }
Vijayakumar Muthuvel Manickam9a9e76e2011-06-30 11:22:25 -0700900 marimba_dbg_remove();
Rahul Kashyapba345d52011-07-14 17:31:53 +0530901 mutex_initialized = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700902 if (pdata->marimba_shutdown != NULL)
903 pdata->marimba_shutdown();
904
905 return 0;
906}
907
908static struct i2c_device_id marimba_id_table[] = {
909 {"marimba", MARIMBA_ID},
910 {"timpani", TIMPANI_ID},
911 {}
912};
913MODULE_DEVICE_TABLE(i2c, marimba_id_table);
914
915static struct i2c_driver marimba_driver = {
916 .driver = {
917 .owner = THIS_MODULE,
918 .name = "marimba-core",
919 },
920 .id_table = marimba_id_table,
921 .probe = marimba_probe,
922 .remove = __devexit_p(marimba_remove),
923};
924
925static int __init marimba_init(void)
926{
927 return i2c_add_driver(&marimba_driver);
928}
929module_init(marimba_init);
930
931static void __exit marimba_exit(void)
932{
933 i2c_del_driver(&marimba_driver);
934}
935module_exit(marimba_exit);
936
937MODULE_DESCRIPTION("Marimba Top level Driver");
938MODULE_ALIAS("platform:marimba-core");
939MODULE_LICENSE("GPL v2");
940MODULE_VERSION("0.1");