Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
switch (audio_swl) {
case 16:
audio_enable |= IT66121_AUD_16BIT;
break;
case 18:
audio_enable |= IT66121_AUD_18BIT;
break;
case 20:
audio_enable |= IT66121_AUD_20BIT;
break;
case 24:
default:
audio_enable |= IT66121_AUD_24BIT;
break;
}
audio_format |= 0x40;
switch (audio_src_num) {
case 4:
audio_enable |= IT66121_AUD_EN_I2S3 | IT66121_AUD_EN_I2S2 |
IT66121_AUD_EN_I2S1 | IT66121_AUD_EN_I2S0;
break;
case 3:
audio_enable |= IT66121_AUD_EN_I2S2 | IT66121_AUD_EN_I2S1 |
IT66121_AUD_EN_I2S0;
break;
case 2:
audio_enable |= IT66121_AUD_EN_I2S1 | IT66121_AUD_EN_I2S0;
break;
case 1:
default:
audio_format &= ~0x40;
audio_enable |= IT66121_AUD_EN_I2S0;
break;
}
audio_format |= 0x01;
ctx->audio.ch_enable = audio_enable;
ret = regmap_write(ctx->regmap, IT66121_AUD_CTRL0_REG, audio_enable & 0xF0);
if (ret)
return ret;
ret = regmap_write(ctx->regmap, IT66121_AUD_CTRL1_REG, audio_format);
if (ret)
return ret;
ret = regmap_write(ctx->regmap, IT66121_AUD_FIFOMAP_REG, 0xE4);
if (ret)
return ret;
ret = regmap_write(ctx->regmap, IT66121_AUD_CTRL3_REG, 0x00);
if (ret)
return ret;
ret = regmap_write(ctx->regmap, IT66121_AUD_SRCVALID_FLAT_REG, 0x00);
if (ret)
return ret;
return regmap_write(ctx->regmap, IT66121_AUD_HDAUDIO_REG, 0x00);
}
static int it661221_set_ncts(struct it66121_ctx *ctx, u8 fs)
{
int ret;
unsigned int n;
switch (fs) {
case IT66121_AUD_FS_32K:
n = 4096;
break;
case IT66121_AUD_FS_44P1K:
n = 6272;
break;
case IT66121_AUD_FS_48K:
n = 6144;
break;
case IT66121_AUD_FS_88P2K:
n = 12544;
break;
case IT66121_AUD_FS_96K:
n = 12288;
break;
case IT66121_AUD_FS_176P4K:
n = 25088;
break;
case IT66121_AUD_FS_192K:
n = 24576;
break;
case IT66121_AUD_FS_768K:
n = 24576;
break;
default:
n = 6144;
break;
}
ret = regmap_write(ctx->regmap, IT66121_AUD_PKT_N0_REG, (u8)((n) & 0xFF));
if (ret)
return ret;
ret = regmap_write(ctx->regmap, IT66121_AUD_PKT_N1_REG, (u8)((n >> 8) & 0xFF));
if (ret)
return ret;
ret = regmap_write(ctx->regmap, IT66121_AUD_PKT_N2_REG, (u8)((n >> 16) & 0xF));
if (ret)
return ret;
if (ctx->audio.auto_cts) {
u8 loop_cnt = 255;
u8 cts_stable_cnt = 0;
unsigned int sum_cts = 0;
unsigned int cts = 0;
unsigned int last_cts = 0;
unsigned int diff;
unsigned int val;
while (loop_cnt--) {
msleep(30);
regmap_read(ctx->regmap, IT66121_AUD_PKT_CTS_CNT2_REG, &val);
cts = val << 12;
regmap_read(ctx->regmap, IT66121_AUD_PKT_CTS_CNT1_REG, &val);
cts |= val << 4;
regmap_read(ctx->regmap, IT66121_AUD_PKT_CTS_CNT0_REG, &val);
cts |= val >> 4;
if (cts == 0) {
continue;
} else {
if (last_cts > cts)
diff = last_cts - cts;
else
diff = cts - last_cts;
last_cts = cts;
if (diff < 5) {
cts_stable_cnt++;
sum_cts += cts;
} else {
cts_stable_cnt = 0;
sum_cts = 0;
continue;
}
if (cts_stable_cnt >= 32) {
last_cts = (sum_cts >> 5);
break;
}
}
}
regmap_write(ctx->regmap, IT66121_AUD_PKT_CTS0_REG, (u8)((last_cts) & 0xFF));
regmap_write(ctx->regmap, IT66121_AUD_PKT_CTS1_REG, (u8)((last_cts >> 8) & 0xFF));
regmap_write(ctx->regmap, IT66121_AUD_PKT_CTS2_REG, (u8)((last_cts >> 16) & 0x0F));
}
ret = regmap_write(ctx->regmap, 0xF8, 0xC3);
if (ret)
return ret;
ret = regmap_write(ctx->regmap, 0xF8, 0xA5);
if (ret)
return ret;
if (ctx->audio.auto_cts) {
ret = regmap_write_bits(ctx->regmap, IT66121_PKT_CTS_CTRL_REG,
IT66121_PKT_CTS_CTRL_SEL,
1);
} else {
ret = regmap_write_bits(ctx->regmap, IT66121_PKT_CTS_CTRL_REG,
IT66121_PKT_CTS_CTRL_SEL,
0);
}
if (ret)
return ret;
return regmap_write(ctx->regmap, 0xF8, 0xFF);
}
static int it661221_audio_output_enable(struct it66121_ctx *ctx, bool enable)
{
int ret;
if (enable) {
ret = regmap_write_bits(ctx->regmap, IT66121_SW_RST_REG,
IT66121_SW_RST_AUD | IT66121_SW_RST_AREF,
0);
if (ret)
return ret;
ret = regmap_write_bits(ctx->regmap, IT66121_AUD_CTRL0_REG,
IT66121_AUD_EN_I2S3 | IT66121_AUD_EN_I2S2 |
IT66121_AUD_EN_I2S1 | IT66121_AUD_EN_I2S0,
ctx->audio.ch_enable);
} else {
ret = regmap_write_bits(ctx->regmap, IT66121_AUD_CTRL0_REG,
IT66121_AUD_EN_I2S3 | IT66121_AUD_EN_I2S2 |
IT66121_AUD_EN_I2S1 | IT66121_AUD_EN_I2S0,
ctx->audio.ch_enable & 0xF0);
if (ret)
return ret;
ret = regmap_write_bits(ctx->regmap, IT66121_SW_RST_REG,
IT66121_SW_RST_AUD | IT66121_SW_RST_AREF,
IT66121_SW_RST_AUD | IT66121_SW_RST_AREF);
}
return ret;
}
static int it661221_audio_ch_enable(struct it66121_ctx *ctx, bool enable)
{
int ret;
if (enable) {
ret = regmap_write(ctx->regmap, IT66121_AUD_SRCVALID_FLAT_REG, 0);
if (ret)
return ret;
ret = regmap_write(ctx->regmap, IT66121_AUD_CTRL0_REG, ctx->audio.ch_enable);
} else {
ret = regmap_write(ctx->regmap, IT66121_AUD_CTRL0_REG, ctx->audio.ch_enable & 0xF0);
}
return ret;
}
static int it66121_audio_hw_params(struct device *dev, void *data,
struct hdmi_codec_daifmt *daifmt,
struct hdmi_codec_params *params)
{
u8 fs;
u8 swl;
int ret;
struct it66121_ctx *ctx = dev_get_drvdata(dev);
static u8 iec60958_chstat[5];
unsigned int channels = params->channels;
unsigned int sample_rate = params->sample_rate;
unsigned int sample_width = params->sample_width;
mutex_lock(&ctx->lock);
dev_dbg(dev, "%s: %u, %u, %u, %u\n", __func__,
daifmt->fmt, sample_rate, sample_width, channels);
switch (daifmt->fmt) {
case HDMI_I2S:
dev_dbg(dev, "Using HDMI I2S\n");
break;
default:
dev_err(dev, "Invalid or unsupported DAI format %d\n", daifmt->fmt);
ret = -EINVAL;
goto out;
}
// Set audio clock recovery (N/CTS)
ret = regmap_write(ctx->regmap, IT66121_CLK_CTRL0_REG,
IT66121_CLK_CTRL0_AUTO_OVER_SAMPLING |
IT66121_CLK_CTRL0_EXT_MCLK_256FS |
IT66121_CLK_CTRL0_AUTO_IPCLK);
if (ret)
goto out;
ret = regmap_write_bits(ctx->regmap, IT66121_AUD_CTRL0_REG,
IT66121_AUD_CTRL0_AUD_SEL, 0); // remove spdif selection
if (ret)
goto out;
switch (sample_rate) {
case 44100L:
fs = IT66121_AUD_FS_44P1K;
break;
case 88200L:
fs = IT66121_AUD_FS_88P2K;
break;
case 176400L:
fs = IT66121_AUD_FS_176P4K;
break;
case 32000L:
fs = IT66121_AUD_FS_32K;
break;
case 48000L:
fs = IT66121_AUD_FS_48K;
break;
case 96000L:
fs = IT66121_AUD_FS_96K;
break;
case 192000L:
fs = IT66121_AUD_FS_192K;
break;
case 768000L:
fs = IT66121_AUD_FS_768K;
break;
default:
fs = IT66121_AUD_FS_48K;
break;
}
ctx->audio.fs = fs;
ret = it661221_set_ncts(ctx, fs);
if (ret) {
dev_err(dev, "Failed to set N/CTS: %d\n", ret);
goto out;
}
// Set audio format register (except audio channel enable)
ret = it661221_set_lpcm_audio(ctx, (channels + 1) / 2, sample_width);
if (ret) {
dev_err(dev, "Failed to set LPCM audio: %d\n", ret);
goto out;
}
// Set audio channel status
iec60958_chstat[0] = 0;
if ((channels + 1) / 2 == 1)
iec60958_chstat[0] |= 0x1;
iec60958_chstat[0] &= ~(1 << 1);
iec60958_chstat[1] = 0;
iec60958_chstat[2] = (channels + 1) / 2;
iec60958_chstat[2] |= (channels << 4) & 0xF0;
iec60958_chstat[3] = fs;
switch (sample_width) {
case 21L:
swl = IT66121_AUD_SWL_21BIT;
break;
case 24L:
swl = IT66121_AUD_SWL_24BIT;
break;
case 23L:
swl = IT66121_AUD_SWL_23BIT;
break;
case 22L:
swl = IT66121_AUD_SWL_22BIT;
break;
case 20L:
swl = IT66121_AUD_SWL_20BIT;
break;
case 17L:
swl = IT66121_AUD_SWL_17BIT;
break;
case 19L:
swl = IT66121_AUD_SWL_19BIT;
break;
case 18L:
swl = IT66121_AUD_SWL_18BIT;
break;
case 16L:
swl = IT66121_AUD_SWL_16BIT;
break;
default:
swl = IT66121_AUD_SWL_NOT_INDICATED;
break;
}
iec60958_chstat[4] = (((~fs) << 4) & 0xF0) | swl;
ret = it661221_set_chstat(ctx, iec60958_chstat);
if (ret) {
dev_err(dev, "Failed to set channel status: %d\n", ret);
goto out;
}
// Enable audio channel enable while input clock stable (if SPDIF).
ret = it661221_audio_ch_enable(ctx, true);
if (ret) {
dev_err(dev, "Failed to enable audio channel: %d\n", ret);
goto out;
}
ret = regmap_write_bits(ctx->regmap, IT66121_INT_MASK1_REG,
IT66121_INT_MASK1_AUD_OVF,
0);
if (ret)
goto out;
dev_dbg(dev, "HDMI audio enabled.\n");
out:
mutex_unlock(&ctx->lock);
return ret;
}
static int it66121_audio_startup(struct device *dev, void *data)
{
int ret;
struct it66121_ctx *ctx = dev_get_drvdata(dev);
dev_dbg(dev, "%s\n", __func__);
mutex_lock(&ctx->lock);
ret = it661221_audio_output_enable(ctx, true);
if (ret)
dev_err(dev, "Failed to enable audio output: %d\n", ret);
mutex_unlock(&ctx->lock);
return ret;
}
static void it66121_audio_shutdown(struct device *dev, void *data)
{
int ret;
struct it66121_ctx *ctx = dev_get_drvdata(dev);
dev_dbg(dev, "%s\n", __func__);
mutex_lock(&ctx->lock);
ret = it661221_audio_output_enable(ctx, false);
if (ret)
dev_err(dev, "Failed to disable audio output: %d\n", ret);
mutex_unlock(&ctx->lock);
}
static int it66121_audio_mute(struct device *dev, void *data,
bool enable, int direction)
{
int ret;
struct it66121_ctx *ctx = dev_get_drvdata(dev);
dev_dbg(dev, "%s: enable=%s, direction=%d\n",
__func__, enable ? "true" : "false", direction);
mutex_lock(&ctx->lock);
if (enable) {
ret = regmap_write_bits(ctx->regmap, IT66121_AUD_SRCVALID_FLAT_REG,
IT66121_AUD_FLAT_SRC0 | IT66121_AUD_FLAT_SRC1 |
IT66121_AUD_FLAT_SRC2 | IT66121_AUD_FLAT_SRC3,
IT66121_AUD_FLAT_SRC0 | IT66121_AUD_FLAT_SRC1 |
IT66121_AUD_FLAT_SRC2 | IT66121_AUD_FLAT_SRC3);
} else {
ret = regmap_write_bits(ctx->regmap, IT66121_AUD_SRCVALID_FLAT_REG,
IT66121_AUD_FLAT_SRC0 | IT66121_AUD_FLAT_SRC1 |
IT66121_AUD_FLAT_SRC2 | IT66121_AUD_FLAT_SRC3,
0);
}
mutex_unlock(&ctx->lock);
return ret;
}
static int it66121_audio_get_eld(struct device *dev, void *data,
u8 *buf, size_t len)
{
struct it66121_ctx *ctx = dev_get_drvdata(dev);
mutex_lock(&ctx->lock);
if (!ctx->connector) {
/* Pass en empty ELD if connector not available */
dev_dbg(dev, "No connector present, passing empty EDID data");
memset(buf, 0, len);
} else {
memcpy(buf, ctx->connector->eld,
min(sizeof(ctx->connector->eld), len));
}
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
mutex_unlock(&ctx->lock);
return 0;
}
static const struct hdmi_codec_ops it66121_audio_codec_ops = {
.hw_params = it66121_audio_hw_params,
.audio_startup = it66121_audio_startup,
.audio_shutdown = it66121_audio_shutdown,
.mute_stream = it66121_audio_mute,
.get_eld = it66121_audio_get_eld,
.no_capture_mute = 1,
};
static int it66121_audio_codec_init(struct it66121_ctx *ctx, struct device *dev)
{
struct hdmi_codec_pdata codec_data = {
.ops = &it66121_audio_codec_ops,
.i2s = 1, /* Only i2s support for now */
.spdif = 0,
.max_i2s_channels = 8,
};
dev_dbg(dev, "%s\n", __func__);
if (!of_property_read_bool(dev->of_node, "#sound-dai-cells")) {
dev_info(dev, "No \"#sound-dai-cells\", no audio\n");
return 0;
}
ctx->audio.pdev = platform_device_register_data(dev,
HDMI_CODEC_DRV_NAME,
PLATFORM_DEVID_AUTO,
&codec_data,
sizeof(codec_data));
if (IS_ERR(ctx->audio.pdev)) {
dev_err(dev, "Failed to initialize HDMI audio codec: %d\n",
PTR_ERR_OR_ZERO(ctx->audio.pdev));
}
return PTR_ERR_OR_ZERO(ctx->audio.pdev);
}
static const char * const it66121_supplies[] = {
"vcn33", "vcn18", "vrf12"
};
static int it66121_probe(struct i2c_client *client)
u32 revision_id, vendor_ids[2] = { 0 }, device_ids[2] = { 0 };
struct device_node *ep;
int ret;
struct it66121_ctx *ctx;
struct device *dev = &client->dev;
if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
dev_err(dev, "I2C check functionality failed.\n");
return -ENXIO;
}
ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
if (!ctx)
return -ENOMEM;
ep = of_graph_get_endpoint_by_regs(dev->of_node, 0, 0);
if (!ep)
return -EINVAL;
ctx->info = i2c_get_match_data(client);
of_property_read_u32(ep, "bus-width", &ctx->bus_width);
of_node_put(ep);
if (ctx->bus_width != 12 && ctx->bus_width != 24)
return -EINVAL;
ep = of_graph_get_remote_node(dev->of_node, 1, -1);
if (!ep) {
dev_err(ctx->dev, "The endpoint is unconnected\n");
return -EINVAL;
}
if (!of_device_is_available(ep)) {
of_node_put(ep);
dev_err(ctx->dev, "The remote device is disabled\n");
return -ENODEV;
}
ctx->next_bridge = of_drm_find_bridge(ep);
of_node_put(ep);
if (!ctx->next_bridge) {
dev_dbg(ctx->dev, "Next bridge not found, deferring probe\n");
return -EPROBE_DEFER;
}
i2c_set_clientdata(client, ctx);
mutex_init(&ctx->lock);
ret = devm_regulator_bulk_get_enable(dev, ARRAY_SIZE(it66121_supplies),
it66121_supplies);
dev_err(dev, "Failed to enable power supplies\n");
return ret;
}
it66121_hw_reset(ctx);
ctx->regmap = devm_regmap_init_i2c(client, &it66121_regmap_config);
if (IS_ERR(ctx->regmap))
return PTR_ERR(ctx->regmap);
regmap_read(ctx->regmap, IT66121_VENDOR_ID0_REG, &vendor_ids[0]);
regmap_read(ctx->regmap, IT66121_VENDOR_ID1_REG, &vendor_ids[1]);
regmap_read(ctx->regmap, IT66121_DEVICE_ID0_REG, &device_ids[0]);
regmap_read(ctx->regmap, IT66121_DEVICE_ID1_REG, &device_ids[1]);
/* Revision is shared with DEVICE_ID1 */
revision_id = FIELD_GET(IT66121_REVISION_MASK, device_ids[1]);
device_ids[1] &= IT66121_DEVICE_ID1_MASK;
if ((vendor_ids[1] << 8 | vendor_ids[0]) != ctx->info->vid ||
(device_ids[1] << 8 | device_ids[0]) != ctx->info->pid) {
return -ENODEV;
}
ctx->bridge.funcs = &it66121_bridge_funcs;
ctx->bridge.of_node = dev->of_node;
ctx->bridge.type = DRM_MODE_CONNECTOR_HDMIA;
ctx->bridge.ops = DRM_BRIDGE_OP_DETECT | DRM_BRIDGE_OP_EDID | DRM_BRIDGE_OP_HPD;
ret = devm_request_threaded_irq(dev, client->irq, NULL, it66121_irq_threaded_handler,
IRQF_ONESHOT, dev_name(dev), ctx);
if (ret < 0) {
dev_err(dev, "Failed to request irq %d:%d\n", client->irq, ret);
return ret;
}
it66121_audio_codec_init(ctx, dev);
drm_bridge_add(&ctx->bridge);
dev_info(ctx->dev, "IT66121 revision %d probed\n", revision_id);
return 0;
}
static void it66121_remove(struct i2c_client *client)
{
struct it66121_ctx *ctx = i2c_get_clientdata(client);
drm_bridge_remove(&ctx->bridge);
mutex_destroy(&ctx->lock);
}
static const struct it66121_chip_info it66121_chip_info = {
.vid = 0x4954,
.pid = 0x0612,
};
static const struct it66121_chip_info it6610_chip_info = {
.id = ID_IT6610,
.vid = 0xca00,
.pid = 0x0611,
};
static const struct of_device_id it66121_dt_match[] = {
{ .compatible = "ite,it66121", &it66121_chip_info },
{ .compatible = "ite,it6610", &it6610_chip_info },
{ }
};
MODULE_DEVICE_TABLE(of, it66121_dt_match);
static const struct i2c_device_id it66121_id[] = {
{ "it66121", (kernel_ulong_t) &it66121_chip_info },
{ "it6610", (kernel_ulong_t) &it6610_chip_info },
{ }
};
MODULE_DEVICE_TABLE(i2c, it66121_id);
static struct i2c_driver it66121_driver = {
.driver = {
.name = "it66121",
.of_match_table = it66121_dt_match,
},
.probe = it66121_probe,