blob: dfe11f71b2109511834e658fa9af8a66b4fab4cc [file] [log] [blame]
Duy Truong790f06d2013-02-13 16:38:12 -08001/* Copyright (c) 2009-2012, The Linux Foundation. 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
Simmi Pateriya4fd69932012-10-26 00:57:06 +0530176 memset(mask_value, 0, sizeof(mask_value));
177
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700178 marimba = &marimba_modules[marimba->mod_id];
Venkateshwarlu Domakonda00c882d2012-04-12 10:50:53 +0530179 if (marimba == NULL) {
180 pr_err("%s: Unable to access Marimba core\n", __func__);
181 return -ENODEV;
182 }
183
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700184 mutex_lock(&marimba->xfer_lock);
185
186 for (i = 0; i < num_bytes; i++)
187 mask_value[i] = (marimba_shadow[marimba->mod_id][reg + i]
188 & ~mask) | (value[i] & mask);
189
190 msg = &marimba->xfer_msg[0];
Venkateshwarlu Domakonda00c882d2012-04-12 10:50:53 +0530191 if (marimba->client == NULL) {
192 pr_err("%s: Unable to access the Marimba slave device.\n",
193 __func__);
194 return -ENODEV;
195 }
196
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700197 msg->addr = marimba->client->addr;
198 msg->flags = 0;
199 msg->len = num_bytes + 1;
200 msg->buf = data;
201 data[0] = reg;
202 memcpy(data+1, mask_value, num_bytes);
203
204 ret = i2c_transfer(marimba->client->adapter, marimba->xfer_msg, 1);
205
206 /* Try again if the write fails */
207 if (ret != 1)
208 ret = i2c_transfer(marimba->client->adapter,
209 marimba->xfer_msg, 1);
210
211 if (ret == 1) {
212 for (i = 0; i < num_bytes; i++)
213 marimba_shadow[marimba->mod_id][reg + i]
214 = mask_value[i];
Ankit Vermad7834992011-08-22 12:29:33 +0530215 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700216 dev_err(&marimba->client->dev, "i2c write failed\n");
Ankit Vermad7834992011-08-22 12:29:33 +0530217 ret = -ENODEV;
218 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700219
220 mutex_unlock(&marimba->xfer_lock);
221
222 return ret;
223}
224EXPORT_SYMBOL(marimba_write_bit_mask);
225
226/**
227 * marimba_write - Sets n bit register in Marimba
228 * @param marimba: marimba structure pointer passed by client
229 * @param reg: register address
230 * @param value: buffer values to be written
231 * @param num_bytes: n bytes to write
232 *
233 * @returns result of the operation.
234 */
235int marimba_write(struct marimba *marimba, u8 reg, u8 *value,
236 unsigned num_bytes)
237{
238 return marimba_write_bit_mask(marimba, reg, value, num_bytes, 0xff);
239}
240EXPORT_SYMBOL(marimba_write);
241
242/**
243 * marimba_read_bit_mask - Reads a n bit register based on bit mask
244 * @param marimba: marimba structure pointer passed by client
245 * @param reg: register address
246 * @param value: i2c read of the register to be stored
247 * @param num_bytes: n bytes to be read.
248 * @param mask: bit mask concerning its register
249 *
250 * @returns result of the operation.
251*/
252int marimba_read_bit_mask(struct marimba *marimba, u8 reg, u8 *value,
253 unsigned num_bytes, u8 mask)
254{
255 int ret, i;
256
257 struct i2c_msg *msg;
258
259 marimba = &marimba_modules[marimba->mod_id];
260
261 mutex_lock(&marimba->xfer_lock);
262
263 msg = &marimba->xfer_msg[0];
264 msg->addr = marimba->client->addr;
265 msg->len = 1;
266 msg->flags = 0;
267 msg->buf = &reg;
268
269 msg = &marimba->xfer_msg[1];
270 msg->addr = marimba->client->addr;
271 msg->len = num_bytes;
272 msg->flags = I2C_M_RD;
273 msg->buf = value;
274
275 ret = i2c_transfer(marimba->client->adapter, marimba->xfer_msg, 2);
276
277 /* Try again if read fails first time */
278 if (ret != 2)
279 ret = i2c_transfer(marimba->client->adapter,
280 marimba->xfer_msg, 2);
281
282 if (ret == 2) {
283 for (i = 0; i < num_bytes; i++) {
284 marimba_shadow[marimba->mod_id][reg + i] = value[i];
285 value[i] &= mask;
286 }
Ankit Vermad7834992011-08-22 12:29:33 +0530287 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700288 dev_err(&marimba->client->dev, "i2c read failed\n");
Ankit Vermad7834992011-08-22 12:29:33 +0530289 ret = -ENODEV;
290 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700291
292 mutex_unlock(&marimba->xfer_lock);
293
294 return ret;
295}
296EXPORT_SYMBOL(marimba_read_bit_mask);
297
298/**
299 * marimba_read - Reads n bit registers in Marimba
300 * @param marimba: marimba structure pointer passed by client
301 * @param reg: register address
302 * @param value: i2c read of the register to be stored
303 * @param num_bytes: n bytes to read.
304 * @param mask: bit mask concerning its register
305 *
306 * @returns result of the operation.
307*/
308int marimba_read(struct marimba *marimba, u8 reg, u8 *value, unsigned num_bytes)
309{
310 return marimba_read_bit_mask(marimba, reg, value, num_bytes, 0xff);
311}
312EXPORT_SYMBOL(marimba_read);
313
314int timpani_read(struct marimba *marimba, u8 reg, u8 *value, unsigned num_bytes)
315{
316 return marimba_read_bit_mask(marimba, reg, value, num_bytes, 0xff);
317}
318EXPORT_SYMBOL(timpani_read);
319
320int timpani_write(struct marimba *marimba, u8 reg,
321 u8 *value, unsigned num_bytes)
322{
323 return marimba_write_bit_mask(marimba, reg, value, num_bytes, 0xff);
324}
325EXPORT_SYMBOL(timpani_write);
326
327static int cur_codec_type = -1, cur_adie_type = -1, cur_connv_type = -1;
328static int adie_arry_idx;
329
330int adie_get_detected_codec_type(void)
331{
332 return cur_codec_type;
333}
334EXPORT_SYMBOL(adie_get_detected_codec_type);
335
336int adie_get_detected_connectivity_type(void)
337{
338 return cur_connv_type;
339}
340EXPORT_SYMBOL(adie_get_detected_connectivity_type);
341
342static struct device *
343add_numbered_child(unsigned chip, const char *name, int num, u8 driver_data,
344 void *pdata, unsigned pdata_len)
345{
346 struct platform_device *pdev;
347 struct marimba *marimba = &marimba_modules[chip + adie_arry_idx];
348 int status = 0;
349
350 pdev = platform_device_alloc(name, num);
351 if (!pdev) {
352 status = -ENOMEM;
353 return ERR_PTR(status);
354 }
355
356 pdev->dev.parent = &marimba->client->dev;
357
358 marimba->mod_id = chip + adie_arry_idx;
359
360 platform_set_drvdata(pdev, marimba);
361
362 if (pdata) {
363 status = platform_device_add_data(pdev, pdata, pdata_len);
364 if (status < 0)
365 goto err;
366 }
367
368 status = platform_device_add(pdev);
369 if (status < 0)
370 goto err;
371
372err:
373 if (status < 0) {
374 platform_set_drvdata(pdev, NULL);
375 platform_device_put(pdev);
376 dev_err(&marimba->client->dev, "can't add %s dev\n", name);
377 return ERR_PTR(status);
378 }
379 return &pdev->dev;
380}
381
382static inline struct device *add_child(unsigned chip, const char *name,
383 u8 driver_data, void *pdata, unsigned pdata_len)
384{
385 return add_numbered_child(chip, name, -1, driver_data, pdata,
386 pdata_len);
387}
388
389static int marimba_add_child(struct marimba_platform_data *pdata,
390 u8 driver_data)
391{
392 struct device *child;
393
394 if (cur_adie_type == MARIMBA_ID) {
395 child = add_child(MARIMBA_SLAVE_ID_FM, "marimba_fm",
396 driver_data, pdata->fm, sizeof(*pdata->fm));
397 if (IS_ERR(child))
398 return PTR_ERR(child);
399 } else if ((cur_adie_type == BAHAMA_ID) &&
400 (cur_connv_type == BAHAMA_ID)) {
401 child = add_child(BAHAMA_SLAVE_ID_FM_ID, "marimba_fm",
402 driver_data, pdata->fm, sizeof(*pdata->fm));
403 if (IS_ERR(child))
404 return PTR_ERR(child);
405 }
406
407 /* Add Codec for Marimba and Timpani */
408 if (cur_adie_type == MARIMBA_ID) {
409 child = add_child(MARIMBA_SLAVE_ID_CDC, "marimba_codec",
410 driver_data, pdata->codec, sizeof(*pdata->codec));
411 if (IS_ERR(child))
412 return PTR_ERR(child);
413 } else if (cur_adie_type == TIMPANI_ID) {
414 child = add_child(MARIMBA_SLAVE_ID_CDC, "timpani_codec",
415 driver_data, pdata->codec, sizeof(*pdata->codec));
416 if (IS_ERR(child))
417 return PTR_ERR(child);
418 }
419
420#if defined(CONFIG_I2C_SSBI)
421 if ((pdata->tsadc != NULL) && (cur_adie_type != BAHAMA_ID)) {
422 child = add_child(MARIMBA_ID_TSADC, "marimba_tsadc",
423 driver_data, pdata->tsadc, sizeof(*pdata->tsadc));
424 if (IS_ERR(child))
425 return PTR_ERR(child);
426 }
427#endif
428 return 0;
429}
430
431int marimba_gpio_config(int gpio_value)
432{
433 struct marimba *marimba = &marimba_modules[MARIMBA_SLAVE_ID_MARIMBA];
434 struct marimba_platform_data *pdata = marimba_pdata;
435 int rc = 0;
436
437 /* Clients BT/FM need to manage GPIO 34 on Fusion for its clocks */
438
439 mutex_lock(&marimba->xfer_lock);
440
441 if (gpio_value) {
442 marimba_gpio_count++;
443 if (marimba_gpio_count == 1)
444 rc = pdata->marimba_gpio_config(1);
445 } else {
446 marimba_gpio_count--;
447 if (marimba_gpio_count == 0)
448 rc = pdata->marimba_gpio_config(0);
449 }
450
451 mutex_unlock(&marimba->xfer_lock);
452
453 return rc;
454
455}
456EXPORT_SYMBOL(marimba_gpio_config);
457
458bool marimba_get_fm_status(struct marimba *marimba)
459{
460 bool ret;
461
462 marimba = &marimba_modules[marimba->mod_id];
463
464 mutex_lock(&marimba->xfer_lock);
465
466 ret = fm_status;
467
468 mutex_unlock(&marimba->xfer_lock);
469
470 return ret;
471}
472EXPORT_SYMBOL(marimba_get_fm_status);
473
474void marimba_set_fm_status(struct marimba *marimba, bool value)
475{
476 marimba = &marimba_modules[marimba->mod_id];
477
478 mutex_lock(&marimba->xfer_lock);
479
480 fm_status = value;
481
482 mutex_unlock(&marimba->xfer_lock);
483}
484EXPORT_SYMBOL(marimba_set_fm_status);
485
486bool marimba_get_bt_status(struct marimba *marimba)
487{
488 bool ret;
489
490 marimba = &marimba_modules[marimba->mod_id];
491
492 mutex_lock(&marimba->xfer_lock);
493
494 ret = bt_status;
495
496 mutex_unlock(&marimba->xfer_lock);
497
498 return ret;
499}
500EXPORT_SYMBOL(marimba_get_bt_status);
501
502void marimba_set_bt_status(struct marimba *marimba, bool value)
503{
504 marimba = &marimba_modules[marimba->mod_id];
505
506 mutex_lock(&marimba->xfer_lock);
507
508 bt_status = value;
509
510 mutex_unlock(&marimba->xfer_lock);
511}
512EXPORT_SYMBOL(marimba_set_bt_status);
513
Vijayakumar Muthuvel Manickam9a9e76e2011-06-30 11:22:25 -0700514#if defined(CONFIG_DEBUG_FS)
515
516static int check_addr(int addr, const char *func_name)
517{
518 if (addr < 0 || addr > 0xFF) {
519 pr_err("%s: Marimba register address is invalid: %d\n",
520 func_name, addr);
521 return -EINVAL;
522 }
523 return 0;
524}
525
526static int marimba_debugfs_set(void *data, u64 val)
527{
528 struct adie_dbg_device *dbgdev = data;
529 u8 reg = val;
530 int rc;
531 struct marimba marimba_id;
532
533 mutex_lock(&dbgdev->dbg_mutex);
534
535 rc = check_addr(dbgdev->addr, __func__);
536 if (rc)
537 goto done;
538
539 marimba_id.mod_id = dbgdev->mod_id;
540 rc = marimba_write(&marimba_id, dbgdev->addr, &reg, 1);
541 rc = (rc == 1) ? 0 : rc;
542
543 if (rc)
544 pr_err("%s: FAIL marimba_write(0x%03X)=0x%02X: rc=%d\n",
545 __func__, dbgdev->addr, reg, rc);
546done:
547 mutex_unlock(&dbgdev->dbg_mutex);
548 return rc;
549}
550
551static int marimba_debugfs_get(void *data, u64 *val)
552{
553 struct adie_dbg_device *dbgdev = data;
554 int rc;
555 u8 reg;
556 struct marimba marimba_id;
557
558 mutex_lock(&dbgdev->dbg_mutex);
559
560 rc = check_addr(dbgdev->addr, __func__);
561 if (rc)
562 goto done;
563
564 marimba_id.mod_id = dbgdev->mod_id;
565 rc = marimba_read(&marimba_id, dbgdev->addr, &reg, 1);
566 rc = (rc == 2) ? 0 : rc;
567
568 if (rc) {
569 pr_err("%s: FAIL marimba_read(0x%03X)=0x%02X: rc=%d\n",
570 __func__, dbgdev->addr, reg, rc);
571 goto done;
572 }
573
574 *val = reg;
575done:
576 mutex_unlock(&dbgdev->dbg_mutex);
577 return rc;
578}
579
580DEFINE_SIMPLE_ATTRIBUTE(dbg_marimba_fops, marimba_debugfs_get,
581 marimba_debugfs_set, "0x%02llX\n");
582
583static int addr_set(void *data, u64 val)
584{
585 struct adie_dbg_device *dbgdev = data;
586 int rc;
587
588 rc = check_addr(val, __func__);
589 if (rc)
590 return rc;
591
592 mutex_lock(&dbgdev->dbg_mutex);
593 dbgdev->addr = val;
594 mutex_unlock(&dbgdev->dbg_mutex);
595
596 return 0;
597}
598
599static int addr_get(void *data, u64 *val)
600{
601 struct adie_dbg_device *dbgdev = data;
602 int rc;
603
604 mutex_lock(&dbgdev->dbg_mutex);
605
606 rc = check_addr(dbgdev->addr, __func__);
607 if (rc) {
608 mutex_unlock(&dbgdev->dbg_mutex);
609 return rc;
610 }
611 *val = dbgdev->addr;
612
613 mutex_unlock(&dbgdev->dbg_mutex);
614
615 return 0;
616}
617
618DEFINE_SIMPLE_ATTRIBUTE(dbg_addr_fops, addr_get, addr_set, "0x%03llX\n");
619
620static int __devinit marimba_dbg_init(int adie_type)
621{
622 struct adie_dbg_device *dbgdev;
Simmi Pateriya4fd69932012-10-26 00:57:06 +0530623 struct dentry *dent = NULL;
Vijayakumar Muthuvel Manickam9a9e76e2011-06-30 11:22:25 -0700624 struct dentry *temp;
625
626 dbgdev = kzalloc(sizeof *dbgdev, GFP_KERNEL);
627 if (dbgdev == NULL) {
628 pr_err("%s: kzalloc() failed.\n", __func__);
629 return -ENOMEM;
630 }
631
632 mutex_init(&dbgdev->dbg_mutex);
633 dbgdev->addr = -1;
634
635 if (adie_type == MARIMBA_ID) {
636 marimba_dbg_device = dbgdev;
637 marimba_dbg_device->mod_id = MARIMBA_SLAVE_ID_MARIMBA;
638 dent = debugfs_create_dir("marimba-dbg", NULL);
639 } else if (adie_type == TIMPANI_ID) {
640 timpani_dbg_device = dbgdev;
641 timpani_dbg_device->mod_id = MARIMBA_SLAVE_ID_MARIMBA;
642 dent = debugfs_create_dir("timpani-dbg", NULL);
643 } else if (adie_type == BAHAMA_ID) {
644 bahama_dbg_device = dbgdev;
645 bahama_dbg_device->mod_id = SLAVE_ID_BAHAMA;
646 dent = debugfs_create_dir("bahama-dbg", NULL);
647 }
648 if (dent == NULL || IS_ERR(dent)) {
649 pr_err("%s: ERR debugfs_create_dir: dent=0x%X\n",
650 __func__, (unsigned)dent);
651 kfree(dbgdev);
652 return -ENOMEM;
653 }
654
655 temp = debugfs_create_file("addr", S_IRUSR | S_IWUSR, dent,
656 dbgdev, &dbg_addr_fops);
657 if (temp == NULL || IS_ERR(temp)) {
658 pr_err("%s: ERR debugfs_create_file: dent=0x%X\n",
659 __func__, (unsigned)temp);
660 goto debug_error;
661 }
662
663 temp = debugfs_create_file("data", S_IRUSR | S_IWUSR, dent,
664 dbgdev, &dbg_marimba_fops);
665 if (temp == NULL || IS_ERR(temp)) {
666 pr_err("%s: ERR debugfs_create_file: dent=0x%X\n",
667 __func__, (unsigned)temp);
668 goto debug_error;
669 }
670 dbgdev->dent = dent;
671
672 return 0;
673
674debug_error:
675 kfree(dbgdev);
676 debugfs_remove_recursive(dent);
677 return -ENOMEM;
678}
679
680static int __devexit marimba_dbg_remove(void)
681{
682 if (marimba_dbg_device) {
683 debugfs_remove_recursive(marimba_dbg_device->dent);
684 kfree(marimba_dbg_device);
685 }
686 if (timpani_dbg_device) {
687 debugfs_remove_recursive(timpani_dbg_device->dent);
688 kfree(timpani_dbg_device);
689 }
690 if (bahama_dbg_device) {
691 debugfs_remove_recursive(bahama_dbg_device->dent);
692 kfree(bahama_dbg_device);
693 }
694 return 0;
695}
696
697#else
698
699static int __devinit marimba_dbg_init(int adie_type)
700{
701 return 0;
702}
703
704static int __devexit marimba_dbg_remove(void)
705{
706 return 0;
707}
708
709#endif
710
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700711static int get_adie_type(void)
712{
713 u8 rd_val;
714 int ret;
715
716 struct marimba *marimba = &marimba_modules[ADIE_ARRY_SIZE - 1];
717
718 marimba->mod_id = ADIE_ARRY_SIZE - 1;
719 /* Enable the Mode for Marimba/Timpani */
720 ret = marimba_read(marimba, MARIMBA_MODE_REG, &rd_val, 1);
721
722 if (ret >= 0) {
723 if (rd_val & 0x80) {
724 cur_adie_type = BAHAMA_ID;
725 return cur_adie_type;
726 } else {
727 ret = marimba_read(marimba,
728 MARIMBA_VERSION_REG, &rd_val, 1);
729 if ((ret >= 0) && (rd_val & 0x20)) {
730 cur_adie_type = TIMPANI_ID;
731 return cur_adie_type;
732 } else if (ret >= 0) {
733 cur_adie_type = MARIMBA_ID;
734 return cur_adie_type;
735 }
736 }
737 }
738
739 return ret;
740}
741
742static void marimba_init_reg(struct i2c_client *client, u8 driver_data)
743{
744 struct marimba_platform_data *pdata = client->dev.platform_data;
745 struct marimba *marimba =
746 &marimba_modules[MARIMBA_SLAVE_ID_MARIMBA + adie_arry_idx];
747
748 u8 buf[1];
749
750 buf[0] = 0x10;
751
752 if (cur_adie_type != BAHAMA_ID) {
753 marimba->mod_id = MARIMBA_SLAVE_ID_MARIMBA + adie_arry_idx;
754 /* Enable the Mode for Marimba/Timpani */
755 marimba_write(marimba, MARIMBA_MODE, buf, 1);
756 } else if ((cur_adie_type == BAHAMA_ID) &&
757 (cur_connv_type == BAHAMA_ID)) {
758 marimba->mod_id = MARIMBA_SLAVE_ID_MARIMBA + adie_arry_idx;
759 marimba_write(marimba, BAHAMA_SLAVE_ID_FM_ID,
760 &pdata->slave_id[SLAVE_ID_BAHAMA_FM], 1);
761 /* Configure Bahama core registers (AREG & DREG) */
762 /* with optimal values to eliminate power leakage */
763 if (pdata->bahama_core_config != NULL)
764 pdata->bahama_core_config(cur_adie_type);
765 }
766}
767
Stephen Boydaf0be7e2012-04-25 11:52:18 -0700768static int __devinit marimba_probe(struct i2c_client *client,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700769 const struct i2c_device_id *id)
770{
771 struct marimba_platform_data *pdata = client->dev.platform_data;
772 struct i2c_adapter *ssbi_adap;
773 struct marimba *marimba;
774 int i, status, rc, client_loop, adie_slave_idx_offset;
775 int rc_bahama = 0, rc_marimba = 0;
776
777 if (!pdata) {
778 dev_dbg(&client->dev, "no platform data?\n");
Rahul Kashyapba345d52011-07-14 17:31:53 +0530779 status = -EINVAL;
780 goto fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700781 }
782
783 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) == 0) {
784 dev_dbg(&client->dev, "can't talk I2C?\n");
Rahul Kashyapba345d52011-07-14 17:31:53 +0530785 status = -EIO;
786 goto fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700787 }
Rahul Kashyapba345d52011-07-14 17:31:53 +0530788 if (!mutex_initialized) {
789 for (i = 0; i < ADIE_ARRY_SIZE; ++i) {
790 marimba = &marimba_modules[i];
791 mutex_init(&marimba->xfer_lock);
792 }
793 mutex_initialized = 1;
794 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700795 /* First, identify the codec type */
796 if (pdata->marimba_setup != NULL) {
797 rc_marimba = pdata->marimba_setup();
798 if (rc_marimba)
799 pdata->marimba_shutdown();
800 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700801 if (pdata->bahama_setup != NULL &&
802 cur_connv_type != BAHAMA_ID) {
803 rc_bahama = pdata->bahama_setup();
804 if (rc_bahama)
805 pdata->bahama_shutdown(cur_connv_type);
806 }
Rahul Kashyapba345d52011-07-14 17:31:53 +0530807 if (rc_marimba & rc_bahama) {
808 status = -EAGAIN;
809 goto fail;
810 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700811 marimba = &marimba_modules[ADIE_ARRY_SIZE - 1];
812 marimba->client = client;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700813
814 rc = get_adie_type();
815
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700816 if (rc < 0) {
817 if (pdata->bahama_setup != NULL)
818 pdata->bahama_shutdown(cur_adie_type);
819 if (pdata->marimba_shutdown != NULL)
820 pdata->marimba_shutdown();
Rahul Kashyapba345d52011-07-14 17:31:53 +0530821 status = -ENODEV;
822 goto fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700823 }
824
825 if (rc < 2) {
826 adie_arry_idx = 0;
827 adie_slave_idx_offset = 0;
828 client_loop = 0;
829 cur_codec_type = rc;
830 if (cur_connv_type < 0)
831 cur_connv_type = rc;
832 if (pdata->bahama_shutdown != NULL)
833 pdata->bahama_shutdown(cur_connv_type);
834 } else {
835 adie_arry_idx = 5;
836 adie_slave_idx_offset = 5;
837 client_loop = 1;
838 cur_connv_type = rc;
839 }
840
841 marimba = &marimba_modules[adie_arry_idx];
842 marimba->client = client;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700843
844 for (i = 1; i <= (NUM_ADD - client_loop); i++) {
845 /* Skip adding BT/FM for Timpani */
846 if (i == 1 && rc >= 1)
847 i++;
848 marimba = &marimba_modules[i + adie_arry_idx];
849 if (i != MARIMBA_ID_TSADC)
850 marimba->client = i2c_new_dummy(client->adapter,
851 pdata->slave_id[i + adie_slave_idx_offset]);
852 else if (pdata->tsadc_ssbi_adap) {
853 ssbi_adap = i2c_get_adapter(pdata->tsadc_ssbi_adap);
854 marimba->client = i2c_new_dummy(ssbi_adap,
855 0x55);
856 } else
857 ssbi_adap = NULL;
858
859 if (!marimba->client) {
Juffin Alex Varghese1949cbd2012-10-29 18:48:40 +0530860 pr_err("can't attach client %d\n", i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700861 status = -ENOMEM;
862 goto fail;
863 }
864 strlcpy(marimba->client->name, id->name,
865 sizeof(marimba->client->name));
866
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700867 }
868
Vijayakumar Muthuvel Manickam9a9e76e2011-06-30 11:22:25 -0700869 if (marimba_dbg_init(rc) != 0)
870 pr_debug("%s: marimba debugfs init failed\n", __func__);
871
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700872 marimba_init_reg(client, id->driver_data);
873
874 status = marimba_add_child(pdata, id->driver_data);
875
876 marimba_pdata = pdata;
877
878 return 0;
879
880fail:
881 return status;
882}
883
884static int __devexit marimba_remove(struct i2c_client *client)
885{
886 int i;
887 struct marimba_platform_data *pdata;
888
889 pdata = client->dev.platform_data;
Rahul Kashyapba345d52011-07-14 17:31:53 +0530890 for (i = 0; i < ADIE_ARRY_SIZE; i++) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700891 struct marimba *marimba = &marimba_modules[i];
892
893 if (marimba->client && marimba->client != client)
894 i2c_unregister_device(marimba->client);
895
896 marimba_modules[i].client = NULL;
Rahul Kashyapba345d52011-07-14 17:31:53 +0530897 if (mutex_initialized)
898 mutex_destroy(&marimba->xfer_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700899
Rahul Kashyapba345d52011-07-14 17:31:53 +0530900 }
Vijayakumar Muthuvel Manickam9a9e76e2011-06-30 11:22:25 -0700901 marimba_dbg_remove();
Rahul Kashyapba345d52011-07-14 17:31:53 +0530902 mutex_initialized = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700903 if (pdata->marimba_shutdown != NULL)
904 pdata->marimba_shutdown();
905
906 return 0;
907}
908
909static struct i2c_device_id marimba_id_table[] = {
910 {"marimba", MARIMBA_ID},
911 {"timpani", TIMPANI_ID},
912 {}
913};
914MODULE_DEVICE_TABLE(i2c, marimba_id_table);
915
916static struct i2c_driver marimba_driver = {
917 .driver = {
918 .owner = THIS_MODULE,
919 .name = "marimba-core",
920 },
921 .id_table = marimba_id_table,
922 .probe = marimba_probe,
923 .remove = __devexit_p(marimba_remove),
924};
925
926static int __init marimba_init(void)
927{
928 return i2c_add_driver(&marimba_driver);
929}
930module_init(marimba_init);
931
932static void __exit marimba_exit(void)
933{
934 i2c_del_driver(&marimba_driver);
935}
936module_exit(marimba_exit);
937
938MODULE_DESCRIPTION("Marimba Top level Driver");
939MODULE_ALIAS("platform:marimba-core");
940MODULE_LICENSE("GPL v2");
941MODULE_VERSION("0.1");