Sample Code:
Assigning it to Permission Set:
SetupEntityAccess objSEA = new SetupEntityAccess();
objSEA.SetupEntityId = '0CP4x000000Pd5y';//Id of the Custom Permission
objSEA.ParentId = '0PS4x000002YEdd';//Id of the Permission Set
insert objSEA;
Assigning it to Profile:
SetupEntityAccess objSEA = new SetupEntityAccess();
objSEA.SetupEntityId = '0CP4x000000Pd5y';//Id of the Custom Permission
objSEA.ParentId = [ SELECT Id, Name, Profile.Name FROM PermissionSet WHERE Profile.Name = 'System Administrator' ][ 0 ].Id;
insert objSEA;